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
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sat Sep 25, 2004 3:05 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Sep 25, 2004 3:14 pm
Code: Select all
#\їphp\]ї\r\n]*(.*?)ї\r\n]*\ї/php\]#si
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sat Sep 25, 2004 3:24 pm
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Sep 25, 2004 3:31 pm
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";
?>',
)
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sat Sep 25, 2004 3:44 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Sep 25, 2004 3:51 pm
your regex and test is different than mine. post the real ones you are using..
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Sep 25, 2004 3:55 pm
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.
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sat Sep 25, 2004 4:05 pm
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Sep 25, 2004 4:15 pm
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
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sat Sep 25, 2004 4:16 pm
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Sep 25, 2004 4:27 pm
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.
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sat Sep 25, 2004 4:27 pm
alright, thanks for your answers
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Mon Sep 27, 2004 7:38 am
anyone?