micmania1 Posted May 9, 2008 Share Posted May 9, 2008 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 More sharing options...
micah1701 Posted May 9, 2008 Share Posted May 9, 2008 preg_replace is geared towards using Perl Regular Expressions. where as str_replace is for simple search and replace functions. Link to comment https://forums.phpfreaks.com/topic/104910-preg_replace-vs-str_replace/#findComment-536952 Share on other sites More sharing options...
micmania1 Posted May 9, 2008 Author Share Posted May 9, 2008 So there is no reason why, at this moment in time, I need to work out what all the nasty looking preg_replace patterns mean? Link to comment https://forums.phpfreaks.com/topic/104910-preg_replace-vs-str_replace/#findComment-536954 Share on other sites More sharing options...
micah1701 Posted May 9, 2008 Share Posted May 9, 2008 probably not...but when you do, ask questions in this sites Regex board. There are a few geniuses that keep an eye on those threads who always post awesome advice (I'm not one of them) Link to comment https://forums.phpfreaks.com/topic/104910-preg_replace-vs-str_replace/#findComment-537044 Share on other sites More sharing options...
Spaceman-Spiff Posted May 9, 2008 Share Posted May 9, 2008 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. Link to comment https://forums.phpfreaks.com/topic/104910-preg_replace-vs-str_replace/#findComment-537056 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.