...

View Full Version : Highlighting code found with PCRE



Vin0rz
02-09-2006, 05:36 PM
Hey, I'd like to be able to insert code on my site using [ code] and [/code] tags. Everything within those tags should be put withing <span class="code"> and </span>, and I want to use highlight_string() to highlight it. I would like to do this using PCRE, how would I set about doing it? I'm capable of reading PCRE (and if I'm not, I'll study it some more) so if you know the solution, posting the answer code is enough. This has been frustrating me since a long while, so help would be greatly appreciated.

Thanks in advance.

raf
02-10-2006, 08:52 AM
...if you know the solution, posting the answer code is enough. ...
this is not a free coding service.
maybe show us how far you got and then someone cann fill in the blanks...

Vin0rz
02-10-2006, 08:58 AM
this is not a free coding service.
maybe show us how far you got and then someone cann fill in the blanks...

Oh, sure, didn't mean that :P

Anyway, how I tried to do it was using preg_split to filter out everything which was between [ code] and [/code], apply highlight_string(), then use str_replace() to replace the original
code with the <span class="code">highlighted code</span>, but that only removed the code.

As it totally didn't work out, I decided not to post it because I thought that if everyone would start off from where I failed you wouldn't get anywhere. Also, I think this is a very inefficient way of doing this and I supposed there would be a way designed especially for cases like this.

raf
02-10-2006, 09:25 AM
just post the relevant code.

there are enough people around here that are experienced enough to read your code and see what you try to do + most of them are assertive enough to tell you you're doing it completely wrong (if that would turn out to be the case)
if you would also include a sample of possible input and expected output, then we'll have all we need...

Mhtml
02-10-2006, 01:50 PM
It took me a long time to figure out how to achieve it. And it's not as easy as you'd think it should be. Also, it's a potential security risk if you do not properly strip appropriatet chars.

You'll need to do an evaluation on the backreference. Also you will need to use output buffering.

Vin0rz
02-10-2006, 04:02 PM
just post the relevant code.

there are enough people around here that are experienced enough to read your code and see what you try to do + most of them are assertive enough to tell you you're doing it completely wrong (if that would turn out to be the case)
if you would also include a sample of possible input and expected output, then we'll have all we need...


$value = preg_split($pattern, $string);
foreach($value as $content){
$newContent = highlight_string($content);
str_replace($content, $newContent, $string);
}

$pattern = '[' . preg_quote('[code]') . '(.*)' . preg_quote('[/code']) . ']iU';

$newString = preg_replace($pattern, '<span class="code">$1</span>', $string);

I'm not too good at writing PCRE yet, so there may be some errors here, but I tried a lot of variations.

Edit: You know, this forums has an example of what I want: the [PHP] BBCode :P


It took me a long time to figure out how to achieve it. And it's not as easy as you'd think it should be. Also, it's a potential security risk if you do not properly strip appropriatet chars.

You'll need to do an evaluation on the backreference. Also you will need to use output buffering.

I already use output buffering, so I wouldn't mind that.

marek_mar
02-10-2006, 04:36 PM
What does output buffering have to do with this?

Mhtml
02-10-2006, 05:17 PM
Actually, maybe it wasn't syntax highlighting that had me working around the clock.. It was some sort of bb code I remember that much lol. And the buffering had something to do with PHP <= 4.2.0 ...



function highlightstr($arr){
return highlight_string($arr[1],true);
}
$code = preg_replace_callback("/\[php\]((.*))\[\\php\]/is","highlightstr",$str);


[edit:]Actually I think I used the buffering for some sort of tokenizing I had going on with my forum software.

Vin0rz
02-10-2006, 05:21 PM
Actually, maybe it wasn't syntax highlighting that had me working around the clock.. It was some sort of bb code I remember that much lol. And the buffering had something to do with PHP <= 4.2.0 ...



function highlightstr($arr){
return highlight_string($arr[1],true);
}
$code = preg_replace_callback("/\[php\]((.*))\[\\php\]/is","highlightstr",$str);


Looks good, will try after I've cleared up my RAM :P

I'll post the results, thanks!

Vin0rz
02-10-2006, 05:43 PM
OK, I tried it but I can't post the results because I'm having another problem... Bummer.

Vin0rz
02-12-2006, 03:36 PM
Actually, maybe it wasn't syntax highlighting that had me working around the clock.. It was some sort of bb code I remember that much lol. And the buffering had something to do with PHP <= 4.2.0 ...



function highlightstr($arr){
return highlight_string($arr[1],true);
}
$code = preg_replace_callback("/\[php\]((.*))\[\\php\]/is","highlightstr",$str);


[edit:]Actually I think I used the buffering for some sort of tokenizing I had going on with my forum software.

OK... One way or another that didn't work. I did it exactly the same way you did it...


function highlightstr ($arr){
return highlight_string($arr[1], true);
}
$newString = preg_replace_callback($pattern, 'highlightstr', $string);

Mhtml
02-12-2006, 03:37 PM
Worked fine for me.. What PHP version?

Vin0rz
02-12-2006, 03:39 PM
Worked fine for me.. What PHP version?


Five.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum