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:

[code lang=”php”]
preg_replace(pattern,replacement,subject [,limit]);
[/code]

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:

[code lang=”php”]
$str = $_POST[“textbox”];

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

$smiliesReplace = array(
‘,
‘,
);

print preg_replace($bbcodeFind,$bbcodeReplace,$str);[/code]

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:

[code lang=”php”]
$bbcodeFind = array(
‘/\[color\=(.*?)\](.*?)\[\/color\]/is’,
‘/\[b\](.*?)\[\/b\]/is’,
‘/\[i\](.*?)\[\/i\]/is’,
);

$bbcodeReplace = array(
$2‘,
$1‘,
$1‘,
);

print preg_replace($bbcodeFind,$bbcodeReplace,$str);
[/code]

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!

Filed under: PHP, Web ProgrammingTagged with: ,

7 Comments

  1. 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.

  2. 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

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

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

  5. 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.

  6. 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!

  7. Thanks I got your url. Does someone by chance have a backup mirror site or link to a different source? The link surely will not seem to work for me.


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *
Name *
Email *
Website