Jump to content

preg_replace question ...


Recommended Posts

Good morning.

 

If I want a user to be able to enter simple markup into a text field using [tag]xyz[/tag] format so that I can convert it on display to <tag>xyz</tag> format, how do I formulate the preg_replace expressions?

 

i.e.

[b]some text[/b]
[i]some text[/i]
[u]some text[/u]
[url]http://www.www.com[/url]
[email]someone@somewere.com[/email]
[img=http://www.com/image.jpg]

etc ...

 

should become:

 

<b>some text</b>
<i>some text</i>
<u>some text</u>
<a href="http://www.www.com">http://www.www.com</a>
<a href="mailto:someone@somewere.com">someone@somewere.com</a>
<img src="http://www.com/image.jpg" />

etc ...

 

also if I want the user to be able to enter the following:

 

[tm]
[sm]
[r]
[c] 

 

and have the display change those tags to the ascii characters ... would str_replace be the best way to handle those? or would preg_replace also work?

 

Any help would be appreciated as I am trying to learn this stuff and haven't found any good examples that are clear to a beginner.

 

Thanks

 

G.

Link to comment
Share on other sites

preg_replace will work. It can take in arrays as arguments so you can just put up 2 arrays. First array is the regexp and the second array is the replacement. So for example:

 

$bb = array(
     '#\[b\](.+?)\[/b\]#i',
     '#\[i\](.+?)\[/i\]#i',
);

$replacement = array(
     '<b>$1</b>',
     '<i>$1</i>',
);

$new_str = preg_replace($bb, $replacement, $str);

Link to comment
Share on other sites

Here's something I have laying around

 

<?php

//bbcode

$patterns = array(

  //BB Code
  "/\[[b]\](.*?)\[\/b\]/is",
  "/\[[i]\](.*?)\[\/i\]/is",
  "/\[[u]\](.*?)\[\/u\]/is",
  "/\[[s]\](.*?)\[\/s\]/is",
  "/\[marquee\](.*?)\[\/marquee\]/is",
  "/\[url\](.*?)\[\/url\]/is",
  "/\[url=(.*?)\](.*?)\[\/url\]/is",
  "/\[img\](.*?)\[\/img\]/is",
  "/\[quote\](.*?)\[\/quote\]/is",
  "/\[code\](.*?)\[\/code\]/is",
  "/\[(size|color)=(.*?)\](.*?)\[\/\\1\]/is",
  "/\[br\]/i",

  //Emoticons
  "/ \:\) /",
  "/ \:\( /",
  "/ \ /",
  "/ \ /",
  "/ \:\| /",
  "/ \ /",
  "/ \:\? /",
  "/ \;\) /");

$replacements = array(

  //BB Code
  "<b>\\1</b>",
  "<i>\\1</i>",
  "<u>\\1</u>",
  "<s>\\1</s>",
  "<marquee>\\1</marquee>",
  "<a href=\"\\1\">\\1</a>",
  "<a href=\"\\1\" target=\"_blank\">\\2</a>",
  "<img border=\"0\" src=\"\\1\">",
  "<div><b>Quote:</b> <i>\\1</i></div>",
  "<br /><b>Code:</b><br /><div style=\"overflow:auto;\"><xmp>\\1</xmp></div><br />",
  "<font \\1=\"\\2\">\\3</font>",
  "<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\"> "
);

$string = "[b]This is bold text[/b] [i]this is italic text[/i] [b][i]This is bold, italic text[/b][/i]";

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

echo $result;

?>[/ode]

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.