Page 3 of 13

Posted: Mon May 28, 2007 8:25 am
by ziggy3000
when i am trying that, it parses
'color:green'
> =
unhighlighted instead of
=
here is the code i am parsing

Code: Select all

$sql = preg_replace('/(=)/', "<div style='color:green'> \\1 </div>", $sql);

Posted: Mon May 28, 2007 9:27 am
by superdezign
Umm... "color: green" isn't easy to differentiate from black on a white background. The code you have works fine for me.

Maybe you want "background-color: green; color: white;" :wink:

And you'll probably want to use span elements instead of div elements.

Posted: Mon May 28, 2007 9:32 am
by ziggy3000
what code are you parsing

i tried parsing this
SELECT *FROM blah WHERE `me` = '$you'

Posted: Mon May 28, 2007 9:35 am
by superdezign

Code: Select all

echo preg_replace('/(=)/', '<span style="background-color: green; color: white;">\\1</span>', "hi = bye when me = gone");

Posted: Mon May 28, 2007 9:39 am
by ziggy3000
now when i am trying to highlight all of the code, only the equal sign get's highlighted :(

Posted: Mon May 28, 2007 9:42 am
by superdezign
Yes... The equal sign is the only thing that you put into the regular expression.

Posted: Mon May 28, 2007 6:22 pm
by ziggy3000
no, i meant that i have 10 more lines that replace and highlight the code, but now only the equal sign is getting highlighted, not the rest of the lines that i coded to replace & highlight

what i meant by that is i have the = replacer
and other code that replace words for it to be highlighted, but now, only the equal sign works. :(

Posted: Mon May 28, 2007 6:30 pm
by John Cartwright
last time I'm going to say this, post your code.

Posted: Mon May 28, 2007 6:31 pm
by ziggy3000
k.

Code: Select all

<?php
function mysql($sql){

	// highlighting the code
	echo "<style>div
	{
	display: inline;
	}
	\n
	code { padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px }
	</style>\n\n";
	$sql = str_replace("\n", '<br />', $sql);

 	$sql = preg_replace('/(=)/i', "<span style='color: green'> \\1 </span>", $sql);

//	$sql = preg_replace("/(*)/", "<div style='color:brown'> \\1 </div>", $sql);
	$sql = preg_replace("/'(.+?)'/i", "<div style='color:red'> '\\1' </div>", $sql);
	$sql = preg_replace('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', "<div style='color:blue'>$1 $2 </div>", $sql);
	$sql = preg_replace('/.(SELECT|UPDATE)\s+/i', "<div style='color:blue'>\\1 </div>", $sql);
	$sql = preg_replace('/.(MAX|AVG|SUM|COUNT|MIN|FROM|INTO)\s+/i', "<div style='color:green'> \\1 </div>", $sql);
	$sql = preg_replace('/`(.+?)`/i', "<div style='color:red'> `\\1` </div>", $sql);
	$sql = preg_replace('/(ASC|DESC|ORDER BY|LIMIT|LEFT|JOIN|WHERE|MODIFY|CHANGE|DISTINCT)/i', "<div style='color:green'> \\1 </div>", $sql);
	$sql = preg_replace('/(LIKE|NOT LIKE|REGEXP)/i', "<div style='color:orange'> \\1 </div>", $sql);
	$sql = preg_replace('/"(.+?)"/i', "<div style='color:red'> \"\\1\" </div>", $sql);
	$sql = preg_replace('/(INT|VARCHAR|TINYINT|BIGINT|BINARY|BIT|BLOB|BOOL|BOOLEAN|CHAR|CHARACTER'.
'|DATE|DATETIME|DEC|DECIMAL|DOUBLE|ENUM|FLOAT|FLOAT4|FLOAT8|INT1|INT2|INT3|INT4|'.
'INT8|INTEGER|LONGBLOB|LONGTEXT|MEDIUMBLOB|MEDIUMTEXT|MEDIUMINT|MIDDLEINT|NCHAR|'.
'NUMERIC|REAL|SERIAL|SET|SMALLINT|TEXT|TIME|TIMESTAMP|TINYBLOB|TINYTEXT|'.
'VARBINARY|YEAR|PRIMARY|AUTO INCREMENT)/i', "<div style='color:darkred'> \\1 </div>", $sql);
	$sql = preg_replace('/;/i', "<b><div style='color: green;'> ;</div></b>", $sql);
	$sql = preg_replace('/[0-9]{6}/i', "<div style='color: red;'> \\1 </div>", $sql);		
//	$sql = preg_replace('/()/i', "<div style='color:'> \\1 </div>", $sql);		
	//$sql = stripslashes($sql);
	
}
?>

Posted: Tue May 29, 2007 1:22 am
by Chris Corbyn
When you take out all the bits after it does it work?

By the way:

Code: Select all

$sql = preg_replace('/(=)/i', "<span style='color: green'> \\1 </span>", $sql);
Would be a lot faster as:

Code: Select all

$sql = str_replace('=', "<span style='color: green'>=</span>", $sql);
Have you ever considered that what you're replacng is adding "=" itself so if this gets run more than once it will mess up your markup. You'll need to view the source code it's producing.

EDIT: I can see one line further down your code that's going to screw it up:

Code: Select all

$sql = preg_replace("/'(.+?)'/i", "<div style='color:red'> '\\1' </div>", $sql);
That replaces "anything" between ' and ' which will inevitably parse you style='' tags.

Posted: Tue May 29, 2007 1:27 am
by Chris Corbyn
Hmmm, the more I look at your code the more I realise how wrongly you've gone about it. I'd still advise using GeSHi ;)

Because you're replacing the string inline and then looking for further syntax you're creating conflicts by parsing your own code. The correct approach is to tokenize the string with those tokens you're looking for, collected into an array or some other container, then loop through that rebuilding it with your styles applied.

Posted: Tue May 29, 2007 3:07 pm
by ziggy3000
the way you told me to fix me code, now it echos
Warning: preg_replace() [function.preg-replace]: No ending delimiter '=' found in C:\highlighter.php on line 45
so that's why i didn't use that

Posted: Tue May 29, 2007 3:10 pm
by ziggy3000
now i changed it to this


Code: Select all

$sql = str_replace("\n", '<br />', $sql);
	$sql = preg_replace("/'(.+?)'/i", "<div style='color:red'> '\\1' </div>", $sql);
	$sql = preg_replace('/"(.+?)"/i', "<div style='color:red'> \"\\1\" </div>", $sql);
	$sql = preg_replace('/`(.+?)`/i', "<div style='color:red'> `\\1` </div>", $sql);
 	$sql = str_replace('/=/', "<span style='color: green'>=</span>", $sql);
//	$sql = preg_replace("/(*)/", "<div style='color:brown'> \\1 </div>", $sql);
	$sql = preg_replace('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', "<div style='color:blue'>$1 $2 </div>", $sql);
	$sql = preg_replace('/.(SELECT|UPDATE)\s+/i', "<div style='color:blue'>\\1 </div>", $sql);
	$sql = preg_replace('/.(MAX|AVG|SUM|COUNT|MIN|FROM|INTO)\s+/i', "<div style='color:green'> \\1 </div>", $sql);
	$sql = preg_replace('/(ASC|DESC|ORDER BY|LIMIT|LEFT|JOIN|WHERE|MODIFY|CHANGE|DISTINCT)/i', "<div style='color:green'> \\1 </div>", $sql);
	$sql = preg_replace('/(LIKE|NOT LIKE|REGEXP)/i', "<div style='color:orange'> \\1 </div>", $sql);
	$sql = preg_replace('/(INT|VARCHAR|TINYINT|BIGINT|BINARY|BIT|BLOB|BOOL|BOOLEAN|CHAR|CHARACTER'.
'|DATE|DATETIME|DEC|DECIMAL|DOUBLE|ENUM|FLOAT|FLOAT4|FLOAT8|INT1|INT2|INT3|INT4|INT8|INTEGER|'.
'LONGBLOB|LONGTEXT|MEDIUMBLOB|MEDIUMTEXT|MEDIUMINT|MIDDLEINT|NCHAR|NUMERIC|REAL|SERIAL|SET|'.
'SMALLINT|TEXT|TIME|TIMESTAMP|TINYBLOB|TINYTEXT|VARBINARY|YEAR|PRIMARY|AUTO INCREMENT)/i', "<div style='color:darkred'> \\1 </div>", $sql);
	$sql = preg_replace('/;/i', "<b><div style='color: green;'> ;</div></b>", $sql);
	$sql = preg_replace('/[0-9]{6}/i', "<div style='color: red;'> \\1 </div>", $sql);
EDIT: the previous post, i used preg instead of str. sorry

feyd | mmm long strings :roll:

Posted: Wed May 30, 2007 5:07 pm
by ziggy3000
i have noticed that people help me more when i dont post my whole code, because users just wait and see how i solve my problem, and then copy without learning...(most of them) :roll:

Posted: Wed May 30, 2007 6:19 pm
by superdezign
ziggy3000 wrote:i have noticed that people help me more when i dont post my whole code, because users just wait and see how i solve my problem, and then copy without learning...(most of them) :roll:
Kind of cocky to assume that if people aren't replying to you that they must be copying your code. I think it's safe to say that, being a forum of developers, we tend to write our code ourselves. And, so you know, most of us don't dwell on one post for too long, so when you have a long snippet of code (I'll admit), we don't always read the whole thing. It's not plain English, you know. :-p

On a lighter note, I assume you've at least been trying rather than coming back and forth here, hoping for a solution, right?
I don't have a solution to offer you, but I do have a suggestion. Use inline elements.


Is it still not working? What part of it isn't working?