Home » Code, PHP Functions

PHP preg_replace

Preg_replace in PHP uses regular expressions to replace matches in the subject with the given replacement. The syntax of preg_replace is as follows:

preg_replace(pattern,replacement,subject [,limit]);

A good example of using preg_replace is with simple bbcode and smilies. However with more complex bbcode there are other ways that would proove to be much easier and more useful. Let’s say you want to replace smiley code with the image of your smilies.  Here is how you would do it with preg_replace:

$str = $_POST["textbox"];

$smiliesFind = array(
'/:\)/',
'/:P/',
);

$smiliesReplace = array(
'<img src="images/smilies/smile.gif" border="0" />',
'<img src="images/smilies/tongue.gif" border="0" />',
);

print preg_replace($bbcodeFind,$bbcodeReplace,$str);

This will take all of those matches in the input $_POST['textbox'] and replace it with the HTML code of the smiley’s image. Now, why does the find code use slashes like this?

When using regular expressions you need a syntax. The slashes provide that regular expression and the backslashes are needed to escape the parts that could be mistaken as something else in the preg_replace.  This goes for anything you want to escape when executing php code.

We can use preg_replace for some bbcode as well:

$bbcodeFind = array(
'/\[color\=(.*?)\](.*?)\[\/color\]/is',
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
);

$bbcodeReplace = array(
'<font color="$1">$2</font>',
'<strong>$1</strong>',
'<em>$1</em>',
);

print preg_replace($bbcodeFind,$bbcodeReplace,$str);

You can see the use of “(.*?)” in the find array. This will apply the code to any text found inside the tags we supply. This is how it would appear:

Green text!
Bold text!
Italic text!

0

Popularity: 3% [?]

Share/Bookmark this!

6 Comments

  • Blair Torres says:

    I’ve recently started a blog, the info you offer on this website has helped me tremendously. Thank you for all of your time & work.

  • Hello, Excellent page and post! My business is interested in design and style and specifically the styling in the not too distant future. Are you gonna be submitting any kind of articles or blog posts on future ideas as well as design? Most of us always look forward with just about every decade after which attempt to replicate design and style techniques belonging to the previous years like the 50′s and also 70′s before shifting to some modern design and style. The european countries plus the USA appear to copy the style of days gone by, designed for commercial worth before seeking more cutting edge designs for future years. Your thoughts if possible.Respect Jake

  • This is why I keep returning to this blog. I can’t believe all the new content since last time!

  • Your web site is extremely interesting,I need to connect with u,could i sent electronic mail to you?

  • Really amazing and informative blog post here. I just wanted to comment & thank you for posting this. I’ve bookmarked youi blog and I’ll be back to read more in the future my friend! Also nice colors on the layout, it’s really easy on the eyes.

  • gooday there, i just stumbled your web portal on yahoo, and i must tell that you compose exceptionally good via your website. i am actually motivated by the mode that you express yourself, and the message is quality. anyhow, i would also love to know whether you would like to exchange links with my website? i will be to the great extent than happy to reciprocate and drop your link on in the blogroll. waiting for your reply, thanks and enjoy your day!

Leave a reply

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

Side Notes

This entry was posted by on February 15, 2009 at 10:36 AM and filed under Code, PHP Functions category.

You can add your comments or trackback from your own site. To keep you updated to the latest discussion, you can subscribe to these comments via RSS.

Recent Entries

Pages

Recent Comments

Resources

Questions & Answers

Just started! Have not answered any questions.

Tag Cloud

background body CSS database date dropdown email error_reporting favicon file filesize form format function global hosting HTML image Javascript limit link linkbar mysql numbers online option ordinalize pagination pattern photoshop PHP resize rows script search snowing stylesheet switch table thumbnail time timestamp upload validate variables

Sponsors