Forum Moderators: coopster

Message Too Old, No Replies

preg replace

phpBB

         

rokec

3:08 pm on Dec 24, 2006 (gmt 0)

10+ Year Member


I want [color=f35f46]lol[/color] to change to <span style="color: #f35f46">lol</span> with php preg_replace. Can anyone help me?

eelixduppy

3:15 pm on Dec 24, 2006 (gmt 0)



Related Thread [webmasterworld.com].

There may be others on this topic, just search [google.com]:)

rokec

3:24 pm on Dec 24, 2006 (gmt 0)

10+ Year Member



I didn't find any useful answers, so i would like to answer me this question directly.

This actually isn't a phpBB code...

eelixduppy

3:46 pm on Dec 24, 2006 (gmt 0)


>>>This actually isn't a phpBB code

It's very similar so I figured that you'd be able to get a solution from it, but here's a more specific example:

$string = "[color=880000]fish sticks[/color] are cool!";
$pattern = "/\[color=([0-9]{6})\](.*)\[\/color\]/is";
$replace = "<span style='color: \\1'>\\2</span>";
echo preg_replace($pattern,$replace,$string);

Good luck and merry christmas :)

rokec

5:21 pm on Dec 24, 2006 (gmt 0)

10+ Year Member



Really thanks, that's script i wanted...

eelixduppy

5:34 pm on Dec 24, 2006 (gmt 0)



Replace should actually be this:

$replace = "<span style='color: [b]#[/b]\\1'>\\2</span>";

;)

rokec

5:53 pm on Dec 24, 2006 (gmt 0)

10+ Year Member



Can you write similar code for [$n] to transform to {$n}?

Thanks.

eelixduppy

7:13 pm on Dec 24, 2006 (gmt 0)



Yes, and now we are getting back to implementing BBCode ;)

Refer to preg_replace [us2.php.net] Example 2: Using indexed arrays with preg_replace().

The format will be similar to the pattern I wrote above. This thread [webmasterworld.com] has some examples as well as some of the threads in the search from my first post.

rokec

8:31 pm on Dec 24, 2006 (gmt 0)

10+ Year Member



I got it at all, so the topic is now ended. For all those having a problems - there is BBCode to HTML converter:

<?php
function bbcode2html($strInput) {
return preg_replace(
array(
'/\\[url[\\:\\=]((\\"([\\W]*javascript\:[^\\"]*)?([^\\"]*)\\")¦'.
'(([\\W]*javascript\:[^\\]]*)?([^\\]]*)))\\]/ie', '/\\[\\/url\\]/i',
'/\\[b\\]/i', '/\\[\/b\\]/i',
'/\\[i\\]/i', '/\\[\/i\\]/i',
'/\\[quote\\]/i', '/\\[\/quote\\]/i'
),
array(
'\'<a href="\'.(\'$4\'?\'$4\':\'$7\').\'">\'', '</a>',
'<strong>', '</strong>',
'<em>', '</em>',
'<blockquote>', '</blockquote>'
),
$strInput
);
}
?>