Page 1 of 1
RegExp pattern which matches newline
Posted: Sat Sep 25, 2004 3:05 pm
by vigge89
I've constructed a Regular Expression pattern to match bbcode for php and code tags, here's how it looks like:
Code: Select all
#\їphp\](\n*)(.*?)(\n*)\ї/php\]#si
it works, apart from one thing, if i use this string, the second match starts with an linebreak. Here's the string:
Code: Select all
їphp]
<?php
echo "hello world";
?>
ї/php]
What I want to do is to make the leading linebreak disappear from the second match.
Also, since this is the first time creating my own regexp patterns, if I can improve the pattern in any way, please tell me
Thanks in advance.
Posted: Sat Sep 25, 2004 3:14 pm
by feyd
Code: Select all
#\їphp\]ї\r\n]*(.*?)ї\r\n]*\ї/php\]#si
Posted: Sat Sep 25, 2004 3:24 pm
by vigge89
thanks for the reply, master feyd, but it still doesn't work, the linebreak is still there

note: the (.*?) changed to 1, it was number 2 before. also, 2 is empty. no idea if you already knew this or not, just wanted to give you some info

Posted: Sat Sep 25, 2004 3:31 pm
by feyd
I knew the numbers would change, I only put 1 group in mine.

As for it not working, I didn't test it

Let's see here...
Code: Select all
<?php
$test = '[php'.']
<?php
$test = "test";
?>
[/php'.']';
preg_match("#\[php\][\r\n]*(.*?)[\r\n]*\[/php\]#si",$test,$match);
var_export($match);
?>
outputs
Code: Select all
array (
0 => 'їphp]
<?php
$test = "test";
?>
ї/php]',
1 => '<?php
$test = "test";
?>',
)
Posted: Sat Sep 25, 2004 3:44 pm
by vigge89
strange, i get this:
Code: Select all
Array ( ї0] => їphp=title, too cool!]
<?php
echo "testing";
?>
ї/php] ї1] => title, too cool! ї2] =>
<?php
echo "testing";
?>
)
Does it matter on which OS php is ran under? currently WinXP Home here.
Posted: Sat Sep 25, 2004 3:51 pm
by feyd
your regex and test is different than mine. post the real ones you are using..

Posted: Sat Sep 25, 2004 3:55 pm
by feyd
btw, here's the one used for our new ones once the
Code: Select all
?)(.*?)\\4(\|([0-9]+?))?|[0-9]+?))?\])(.*?)(\
)$#is', $text, $matches);$uid is passed in from phpbb, the other variables are self explanitory I hope.
Posted: Sat Sep 25, 2004 4:05 pm
by vigge89
aww, copied the wrong output
alright, here's some various output from different inputs:
Code: Select all
Array ( ї0] => їphp]echo "hello world";ї/php] ї1] => echo "hello world"; )
Array ( ї0] => їphp]<?php echo "hello world"; ?>ї/php] ї1] => <?php echo "hello world"; ?> )
Array ( ї0] => їphp]<?php
echo "hello world";
?>ї/php] ї1] => <?php
echo "hello world";
?> )
Array ( ї0] => їphp]
<?php
echo "hello world";
?>
ї/php] ї1] =>
<?php
echo "hello world";
?>
)
Using your first pattern...
Btw, I didn't find any newline checks in the phpBB pattern, it was rather complicated

Posted: Sat Sep 25, 2004 4:15 pm
by feyd
there aren't any newline things in the regex. it's handled seperately because of some line counting and other special code I had to add.
what exact regex did you use, because the one I posted works perfectly. Post the entire code line please. I'd bet you put the regex pattern in a single quote string, when it should be a double quote string

Posted: Sat Sep 25, 2004 4:16 pm
by vigge89
nope
Code: Select all
// php highlighting
$string = preg_replace_callback ("#\їphp=(.*?)\]ї\r\n]*(.*?)ї\r\n]*\ї/php\]#si", "highlight_title", $string);
$string = preg_replace_callback ("#\їphp\]ї\r\n]*(.*?)ї\r\n]*\ї/php\]#si", "highlight", $string);
// code highlighting
$string = preg_replace_callback ("#\їcode=(.*?)\]ї\r\n]*(.*?)ї\r\n]*\ї/code\]#si", "hl_code_title", $string);
$string = preg_replace_callback ("#\їcode\]ї\r\n]*(.*?)ї\r\n]*\ї/code\]#si", "hl_code", $string);
EDIT: had to change to code-tags to prevent buggys

note: bug is fixed in my syntax highlighter

Posted: Sat Sep 25, 2004 4:27 pm
by feyd
hmm.. dunno how that bug happens.. oh well. I'll eventually get around to it.
not entirely sure what's wrong.. sorry I don't have time to deal with it much right now.
Posted: Sat Sep 25, 2004 4:27 pm
by vigge89
alright, thanks for your answers

Posted: Mon Sep 27, 2004 7:38 am
by vigge89
anyone?