Jump to content

highlight_string and non-English characters


The Little Guy

Recommended Posts

I had a member submit a snippet with Russian characters in his/her comments, when I used highlight_string on the code, nothing came back. As soon as I removed the comments highlight_string worked fine, so I decided to translate them so they would show up on the page, and that they did.

 

So my question: does highlight_string not support Russian, Chinese, etc. characters? If it does how can I fix it so that it will?

 

all I am doing is this:

// $str comes from the database
$assign = " ... " . highlight_string($str, true) . " ... ";
echo $assign;

Link to comment
Share on other sites

Here was the code:

<?php
     /*	Erstellt eine Tabelle aus mysql Rückgabezeilen
	summary:	
				Erstellt eine Tabelle aus mysql Rückgabezeilen
				(Eine einfache Tabelle erreicht man ohne ausfüllen der Argumente, bis auf das Erste)

	Argument 01:	Die Zeilen die ausgegeben werden sollen
	Argument 02:	Die komplette Kopfzeile
				Wird das Argument leer gelassen, so werden die Rückgabespalten als Grundlage verwendet
	Argument 03:	Die komplette Fußzeile
				Wird weggelassen, wenn leer
	Argument 04 und 5:
				colClass und colMethod steht für eine eigene Definition jeden Wertes jeder Spalter in jeder Zeile
				colClass ist der Name der benutzerdefinierten Klasse in welcher sich die Methode befindet
				colMethod1Arg ist der Name der benutzerdefinierten Funktion in der Klasse colClass, die den Spaltenwert bearbeitet, bevor er in der Tabellenzelle steht
				dabei ist zu bedenken, dass diese Funktion/Methode 1 Argument hat, welches ein Array empfängt mit dem Namen der Spalte und dem Datenbankwert
				die benutzerdefnierte Funktion sollte einen Wert zurückgeben, der den angezeigten Wert darstellt
	Argument 06:	tableId
				Id der Tabelle, die verwendet werden soll
	Argument 07:	tableName
				Name der Tabelle, der verwendet werden soll
	Argument 08:	tableAttributes
				Irgendwelche Attribute, wie sie auch in HTML eingetragen würden
	Argument 09:	rowAttributes
				Irgendwelche Attribute, die in jeder Zeile eingetragen werden sollen, wie in HTML verwendet
	Argument 10:	cellAttributes
				Irgendwelche Attribute, die in jeder Spalte/Zelle eingetragen werden sollen, wie in HTML verwendet
	Argument 11:	colWrap = "|"
				eine benutzerdefnierte Formatierung jeden Wertes jeder Zeile
				| stellt dabei den Wert der jeweiligen Zelle dar, wie er regulär dastehen würde, (ggf. nachdem er durch die benutzerdefinierte Funktion gesendet und wieder empfangen wurde)
*/
function makeTable($rows, $Head = "", $Foot = "", $colClass = "", $colMethod1Arg = "", $tableId = "", $tableName = "", $tableAttributes = "", $rowAttributes = "", $cellAttributes = "", $colWrap = "|") {
	$_rows = $Head;

	foreach($rows as $rowIndex => $row) {
		if(empty($Head) && $rowIndex == 0) {
			$_rows = "<tr>\n<td>".implode("</td><td>", array_keys($row))."</td>\n</tr>\n";
		}

		$columns = "";
		$c = 0;
		foreach($row as $colName => $colValue) {
			$colValue = (empty($colClass) || empty($colMethod1Arg) ? $colValue : call_user_func(array($colClass, $colMethod1Arg), array($colName, $colValue)));

			if($colWrap != "|" && !empty($colWrap)) {
				$colValue = str_replace("|", $colValue, $colWrap);
			}

			$columns .= "<td ".
			'id="col-'.$c.'" '.
			'name="col_'.$colName.'" '.
			'class="col_'.$colName.'"'.
			(!empty($cellAttributes) ? " " : "").$cellAttributes." >".
			$colValue."</td>";

			$c++;
		}

		$_rows .= '<tr id="row-'.$rowIndex.'" name="row_'.$rowIndex.'" '.(!empty($rowAttributes) ? " " : "").$rowAttributes." >\n".$columns."\n</tr>";
	}

	$table = '<table '.
	(!empty($tableId) ? 'id="'.$tableId.'" ' : "").
	(!empty($tableName) ? 'name="'.$tableName.'"' : "").
	(!empty($tableAttributes) ? " " : "").$tableAttributes." >".
	$_rows."\n".(!empty($Foot) ? $Foot."\n" : "").
	'</table>';

	return $table;
}

/*	Erweitert die mysql Rückgabezeilen um Spalten, links oder rechts
	summary:
			Erweitert die mysql Rückgabezeilen um Spalten, links oder rechts

	Argument 01:		rows
					Die Zeilen, die erweitert werden sollen
	Argument 02|03:	colConfLeft oder/und colConfRight
					Spaltendefinitionen die links von den RückgabeSpalten angefügt werden sollen mit Zellinhalten in htmlentities
					Beispiel
					Spalte "Auswahl" soll in jeder Zeile eine Checkbox haben
					das Argument sollte dann so aussehen:
						array("Auswahl" => '<input type="checkbox" value="1">Ja</input>');
*/
function append_columns($rows, $colConfLeft = null, $colConfRight = null) {
	if($colConfLeft == null && $colConfRight == null)
		return $rows;

	foreach($rows as $rowIndex => $row) {
		if($colConfLeft != null)
			$rows[$rowIndex] = array_merge($colConfLeft, $row);

		if($colConfRight != null)
			$rows[$rowIndex] = array_merge($row, $colConfRight);
	}

	return $rows;
}

/*	Eine mysql Verbindung mit den Grundeinstellungen
	summary:	Eine mysql Verbindung mit den Grundeinstellungen

	Konstruktor-Argumente:

	Argument 01:	host
				Ort im Netzwerk, wo der Sql-Befehl angewendet werden soll
	Argument 02:	username
				Anmeldename an der mysql Datenbankverbindung
	Argument 03:	password
				Passwort zur mysql Datenbankverbindung
	Argument 04:	fieldnamereplacements
				Ersatztitel von Spalten
*/
class Sql_Connection {
	private $host = "";
	private $username = "";
	private $password = "";
	private $fieldnamereplacements = null;

	public function __construct($host, $username, $password, $fieldnamereplacements = null) {
		$this->host = $host;
		$this->username = $username;
		$this->password = $password;
		$this->fieldnamereplacements  = $fieldnamereplacements;
	}

	public function get_rows($sql) {
		if($this->fieldnamereplacements != null) {
			foreach($this->fieldnamereplacements as $find => $replace) {
				$sql = str_replace($find, $find." AS `".$replace."`", $sql);
			}
		}
		if(!mysql_connect($this->host, $this->username, $this->password))
			die ("Keine Hostverbindung");

		$result = mysql_query($sql);
		mysql_close();

		if(!$result)
			die(mysql_error());

		if(mysql_num_rows($result) == 0)
			return array();

		$rows = array();
		for($rowIndex = 0; $row = mysql_fetch_assoc($result); $rowIndex++) {
			$rows[$rowIndex] = $row;
		}

	return $rows;
}

}

/*	Eine MySQL Abfrage mit Rückmeldung von Datenbankzeilen
	summary:	Eine MySQL Abfrage mit Rückmeldung von Datenbankzeilen

	Argument 01:	host
				Ort, der mysql Datenbank ohne http
	Argument 02:	user
				Benutzername zur Anmeldung an der mysql Datenverbindung
	Argument 03:	password
				Passwort zur Anmeldung an der mysql Datenverbindung
	Argument 04:	sql (ich empfehle die Spaltennamen im SQL Befehl anstelle dem * (Sternchen) zu verwenden, um Argument 05 besser verwenden zu können)
				Der SELECT Befehl zur Wiedergabe der Datenzeilen
	Argument 05:	fieldreplacements
				Ersatztitel für die Spalten. Wird dieses Argument weggelassen, werden die Bezeichnung der Datenbankspalten verwendet
	Argument 06:	sqlObject
				Ein Variable, die die MySqlVerbindung als Objekt zurückbehält
*/
function mysql_rows($host, $user, $password, $sql, $fieldreplacements = null, &$sqlObject = null) {
	$sqlObject = new Sql_Connection($host, $user, $password, $fieldreplacements);
	return $sqlObject->get_rows($sql);
}
?>

 

It seems to work here, do I need to convert the code to utf8 or something?

Link to comment
Share on other sites

You need to TEST to figure out where the problem is. You have a large chunk of text that you say is not outputting anything. Did you try "some" of the text? Start with one section. If it works, add another section, and so on. Once you add a section that causes the process to break then add only the first 1/2 of that section. If it breaks then only add 1/2 of that. If it works then add 1/2 of the next section. It would only take a few minutes to identify the exact line that is causing the problem.

 

But, are you positive you just didn't make a simple error such as not including the right file name? You say it was outputting nothing - which makes no sense. How did you verify that you had loaded the contents of that file? I dumped the content above into a file and used highlight_string() with no problems without doing any encoding at all.

 

You should add some debugging to your code to find these types of problems. Try running this against that file

$code = file_get_contents('somefile.php');

if($code === false)
{
    echo "Unable to read file contents";
}
else
{
    echo "The file contains "  . strlen($code) . " characters.<br><br>";
    echo highlight_string($code, true);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.