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";
?>
&#1111;/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

#\&#1111;php\]&#1111;\r\n]*(.*?)&#1111;\r\n]*\&#1111;/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 :P 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 =&gt; '&#1111;php]
&lt;?php
        $test = "test";
?&gt;
&#1111;/php]',
  1 =&gt; '&lt;?php
        $test = "test";
?&gt;',
)

Posted: Sat Sep 25, 2004 3:44 pm
by vigge89
strange, i get this:

Code: Select all

Array ( &#1111;0] => &#1111;php=title, too cool!]
<?php
echo "testing";
?>
&#1111;/php] &#1111;1] => title, too cool! &#1111;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.. :P

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

tag is found:

Code: Select all

preg_match('#^(\

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 ( &#1111;0] => &#1111;php]echo "hello world";&#1111;/php] &#1111;1] => echo "hello world"; )

Array ( &#1111;0] => &#1111;php]<?php echo "hello world"; ?>&#1111;/php] &#1111;1] => <?php echo "hello world"; ?> )

Array ( &#1111;0] => &#1111;php]<?php
echo "hello world";
?>&#1111;/php] &#1111;1] => <?php
echo "hello world";
?> )

Array ( &#1111;0] => &#1111;php]
<?php
echo "hello world";
?>
&#1111;/php] &#1111;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 :P

Code: Select all

// php highlighting
$string = preg_replace_callback ("#\&#1111;php=(.*?)\]&#1111;\r\n]*(.*?)&#1111;\r\n]*\&#1111;/php\]#si", "highlight_title", $string);
$string = preg_replace_callback ("#\&#1111;php\]&#1111;\r\n]*(.*?)&#1111;\r\n]*\&#1111;/php\]#si", "highlight", $string);
// code highlighting
$string = preg_replace_callback ("#\&#1111;code=(.*?)\]&#1111;\r\n]*(.*?)&#1111;\r\n]*\&#1111;/code\]#si", "hl_code_title", $string);
$string = preg_replace_callback ("#\&#1111;code\]&#1111;\r\n]*(.*?)&#1111;\r\n]*\&#1111;/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?