Forum Moderators: coopster

Message Too Old, No Replies

regex multi line modifier not working

         

Readie

12:34 am on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I decided to re-write my BB code function to reduce some of the overhead incurred by so many nested for loops and if statements.

The 2-piece part of the function below works fine, however the 3-piece (the url ones) just don't work if there's a line break.

I've been staring at this for about an hour now, making small tweaks here and there, and I just cannot get it to work.

Please help :/

function bb_code($input) {

$bb_a_a = array(
'/\[b\](.*?(?!(\[\/?b\])).*?)\[\/b\]/sim',
'/\[u\](.*?(?!(\[\/?u\])).*?)\[\/u\]/sim',
'/\[i\](.*?(?!(\[\/?i\])).*?)\[\/i\]/sim',
'/\[img\](.*?(?!(\[\/?img\])).*?)\[\/img\]/sim'
);

$bb_a_b = array(
array('<b>', '</b>'),
array('<u>', '</u>'),
array('<i>', '</i>'),
array('<img border="0" src="', '">')
);

$a_count = count($bb_a_a);
$b_count = count($bb_a_b);
if($a_count == $b_count) {
for($i = 0; $i < $a_count; $i++) {
if(preg_match($bb_a_a[$i], $input)) {
preg_match_all($bb_a_a[$i], $input, $out, PREG_PATTERN_ORDER);
$count = count($out[0]);
for($ii = 0; $ii < $count; $ii++) {
$repl = $bb_a_b[$i][0] . $out[1][$ii] . $bb_a_b[$i][1];
$input = str_ireplace($out[0][$ii], $repl, $input);
}
}
}
}

$bb_b_a = array(
'/\[url="(.*?(?!(\[url="|\[\/url\])).*?)"\](.*?(?!(\[url="|\[\/url\])).*?)\[\/url\]/sim',
'/\[urlblank="(.*?(?!(\[urlblank="|\[\/urlblank\])).*?)"\](.*?(?!(\[urlblank="|\[\/urlblank\])).*?)\[\/urlblank\]/sim'
);

$bb_b_b = array(
array('<a href="', '">', '</a>'),
array('<a target="blank" href="', '">', '</a>')
);

$a_count = count($bb_b_a);
$b_count = count($bb_b_b);
if($a_count == $b_count) {
for($i = 0; $i < $a_count; $i++) {
if(preg_match($bb_b_a[$i], $input)) {
preg_match_all($bb_b_a[$i], $input, $out, PREG_PATTERN_ORDER);
$count = count($out[0]);
for($i = 0; $i < $count; $i++) {
$repl = $bb_b_b[$i][0] . $out[1][$ii] . $bb_b_b[$i][1] . $out[3][$ii] . $bb_b_b[$i][2];
$input = str_ireplace($out[0][$ii], $repl, $input);
}
}
}
}
return $input;
}

Readie

2:18 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I simplified things a bit, and whilest it didn't work, it did suddenly occur to me that it might be a good thing to not let the user have a single link spanning multiple line breaks.

Functionality remained the same with the following reg exp's:

'/\[b\](.*?)\[\/b\]/mis',
'/\[u\](.*?)\[\/u\]/mis',
'/\[i\](.*?)\[\/i\]/mis',
'/\[img\](.*?)\[\/img\]/is'

'/\[url="(.*?)"\](.*?)\[\/url\]/is',
'/\[urlblank="(.*?)"\](.*?)\[\/urlblank\]/is'

Readie

4:19 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to come back again -

'/\[url=&#34;(.*?)&#34;\](?m)(.*?)(?-m)\[\/url\]/is',
'/\[urlblank=&#34;(.*?)&#34;\](?m)(.*?)(?-m)\[\/urlblank\]/is'


Now has the functionality I originally wanted :)

Matthew1980

4:35 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Readie,

Glad you got what you wanted in the end. I must confess as preg_match() and the associated patterns are not my strong point, so as you have had some success I shall attempt to add something similar to my contact me page, but first I'm adding a preview message first... All good fun!

Cheers,
MRb