RegExp pattern which matches newline

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

RegExp pattern which matches newline

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

#\&#1111;php\]&#1111;\r\n]*(.*?)&#1111;\r\n]*\&#1111;/php\]#si
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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;',
)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your regex and test is different than mine. post the real ones you are using.. :P
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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 ;)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

alright, thanks for your answers :)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

anyone?
Post Reply