Jump to content

preg_replace vs str_replace


micmania1

Recommended Posts

I have a forum which at the moment, has no bbcode. All messages are stored using htmlspecialchars(). The only formatting it does is, parses each message, and replaces '\n' with '<br />'

 

Now I am looking into creating bbcode functions and the tutorials on creating bbcode functions use preg replace.

 

Is there a reason preg_replace is better than str_replace?

 

Can somebody give me examples of when each should be used over the other?

 

Any info is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/104910-preg_replace-vs-str_replace/
Share on other sites

By using regex, you can do pattern matching. It's a much more powerful tool than simple str_replace. Some bb codes can be replaced directly using str_replace, for example for [ b ] or [ /b ] to < b > and < /b >.

 

But for something more complex, like involving attributes, you will need to use regex. For example [ url=http://urllink.com ] text [ /url ]. A simple str_replace won't do.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.