Jump to content

probem with pregreplace


blueman378

Recommended Posts

hi guys, well heres the code im using,

<?php

//bbcode

$patterns = array(

"/\[url\](.*?)\[\/url\]/",
"/\[img\](.*?)\[\/img\]/",
"/\[b\](.*?)\[\/B\]/",
"/\[b\](.*?)\[\/b\]/",
"/\[u\](.*?)\[\/U\]/",
"/\[u\](.*?)\[\/u\]/",
"/\[i\](.*?)\[\/I\]/",
"/\[i\](.*?)\[\/i\]/",
"/\[quote\](.*?)\[\/quote\]/",
"/\[quote\](.*?)\[\/QUOTE\]/",
"/\[code\](.*?)\[\/code\]/",
"/\[code\](.*?)\[\/CODE\]/",
"/\[s\](.*?)\[\/s\]/",
"/\[s\](.*?)\[\/S\]/",
"/\[url=(.*?)\](.*?)\[\/url\]/",
"/\[color=(.*?)\](.*?)\[\/color\]/",
"/\[size=(.*?)\](.*?)\[\/size\]/",
"/\[marquee\](.*?)\[\/marquee\]/",
"/\[br\]/",

//emoticons

"/\:\)/",
"/\:\(/",
"/\:O/",
"/\:P/",
"/\:\|/",
"/\:D/",
"/\:\?/",);

$replacements = array(

//bbcode

"<a href=\"\\1\">\\1</a>",
"<img border=0 src='\\1'>",
"<b>\\1</b>",
"<b>\\1</b>",
"<u>\\1</u>",
"<u>\\1</u>",
"<i>\\1</i>",
"<i>\\1</i>",
"<div><b>Quote:</b> <i>\\1</i></div>",
"<div><b>Quote:</b> \\1</div>",
"<b>Code:</b><div style='line-height: 12px; width: 99%; white-space: nowrap; overflow: auto; max-height: 25em;'>\\1</div>",
"<b>Code:</b><div style='line-height: 12px; width: 99%; white-space: nowrap; overflow: auto; max-height: 25em;'>\\1</div>",
"<s>\\1</s>",
"<s>\\1</s>",
"<a href=\"\\1\" target=\"_blank\">\\2</a>",
"<font color=\"\\1\">\\2</font>",
"<font size=\"\\1\">\\2</font>",
"<marquee>\\1</marquee>",
"<br />",

//emoticons

"<img src=\"smilies/happy.gif\" border=\"0\">",
"<img src=\"smilies/angry.gif\" border=\"0\">",
"<img src=\"smilies/omg.gif\" border=\"0\">",
"<img src=\"smilies/tounge.gif\" border=\"0\">",
"<img src=\"smilies/dry.gif\" border=\"0\">",
"<img src=\"smilies/biggrin.gif\" border=\"0\">",
"<img src=\"smilies/confused.gif\" border=\"0\">"
);



?>

 

which works perfect,

 

i am wanting to add a ;) smiley in however whenever i try it reads it as the end of the array and spits out an error, how would i stop this from doing that? i tried escaping it with a \ but it doesnt seem to be working

Link to comment
Share on other sites

first make sure there is not a comma before then ending bracket like so: ,); will probably call an error.

 

i dont see why its not working i shall test on my system.

 

TESTED.

 

<?php

//bbcode

$patterns = array(

"/\[url\](.*?)\[\/url\]/",
"/\[img\](.*?)\[\/img\]/",
"/\[b\](.*?)\[\/B\]/",
"/\[b\](.*?)\[\/b\]/",
"/\[u\](.*?)\[\/U\]/",
"/\[u\](.*?)\[\/u\]/",
"/\[i\](.*?)\[\/I\]/",
"/\[i\](.*?)\[\/i\]/",
"/\[quote\](.*?)\[\/quote\]/",
"/\[quote\](.*?)\[\/QUOTE\]/",
"/\[code\](.*?)\[\/code\]/",
"/\[code\](.*?)\[\/CODE\]/",
"/\[s\](.*?)\[\/s\]/",
"/\[s\](.*?)\[\/S\]/",
"/\[url=(.*?)\](.*?)\[\/url\]/",
"/\[color=(.*?)\](.*?)\[\/color\]/",
"/\[size=(.*?)\](.*?)\[\/size\]/",
"/\[marquee\](.*?)\[\/marquee\]/",
"/\[br\]/",

//emoticons

"/\:\)/",
"/\:\(/",
"/\:O/",
"/\:P/",
"/\:\|/",
"/\:D/",
"/\:\?/",
"/\;\)/");

$replacements = array(

//bbcode

"<a href=\"\\1\">\\1</a>",
"<img border=0 src='\\1'>",
"<b>\\1</b>",
"<b>\\1</b>",
"<u>\\1</u>",
"<u>\\1</u>",
"<i>\\1</i>",
"<i>\\1</i>",
"<div><b>Quote:</b> <i>\\1</i></div>",
"<div><b>Quote:</b> \\1</div>",
"<b>Code:</b><div style='line-height: 12px; width: 99%; white-space: nowrap; overflow: auto; max-height: 25em;'>\\1</div>",
"<b>Code:</b><div style='line-height: 12px; width: 99%; white-space: nowrap; overflow: auto; max-height: 25em;'>\\1</div>",
"<s>\\1</s>",
"<s>\\1</s>",
"<a href=\"\\1\" target=\"_blank\">\\2</a>",
"<font color=\"\\1\">\\2</font>",
"<font size=\"\\1\">\\2</font>",
"<marquee>\\1</marquee>",
"<br />",

//emoticons

"<img src=\"smilies/happy.gif\" border=\"0\">",
"<img src=\"smilies/angry.gif\" border=\"0\">",
"<img src=\"smilies/omg.gif\" border=\"0\">",
"<img src=\"smilies/tounge.gif\" border=\"0\">",
"<img src=\"smilies/dry.gif\" border=\"0\">",
"<img src=\"smilies/biggrin.gif\" border=\"0\">",
"<img src=\"smilies/confused.gif\" border=\"0\">",
"<img src=\"smilies/wink.gif\" border=\"0\">"
);

?>

 

works perfect for me.

 

used this code:

<?php

$string = "this string  has a smiley in it";

$result = preg_replace($patterns,$replacements,$string);

print_r($result);

?>

to test it

Link to comment
Share on other sites

FYI: You can cut your code nearly in half by making the patterns case insensitive:

 

Instead of

"/\[b\](.*?)\[\/B\]/",
"/\[b\](.*?)\[\/b\]/",

 

Use this, with the "i" parameter to indicate the patwtern should be case insensitive.

"/\[b\](.*?)\[\/b\]/i",

Link to comment
Share on other sites

ah right that makes sense, one more question, this code works as long as the data is in the same line, eg

[ code ] data here [ / code ] (without spaces) works

but

[ code ]

data here

[ / code ] (without spaces) doesnt get replaced, any way to get the replace to span over multiple lines?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.