shamuraq Posted August 15, 2009 Share Posted August 15, 2009 Hi all, I'm trying to replace certain characters i pulled from database using preg_replace. <? $patterns[0] = '<br />'; $replacements[0] = "\n"; echo preg_replace($patterns, $replacements, $sub);?> ?> Resulting output: Theme 1: Science and Technology< > Chapter 1 Introducing Science < > Theme 2: Measurement< > Chapter 2 Measurement and Units< > etc. What i don't understand is why there's "<" and ">" still reflected on the output? Quote Link to comment https://forums.phpfreaks.com/topic/170447-preg_replace-confusion/ Share on other sites More sharing options...
oni-kun Posted August 15, 2009 Share Posted August 15, 2009 Your regex does not match against an expression, only a simple pattern, you can use either of these: function br2nl($text) { return preg_replace('/<br\\s*?\/??>/i', '', $text); } br2nl($sub); //Using a function echo preg_replace('/<br\\s*?\/??>/i', '', $sub); //Directly Quote Link to comment https://forums.phpfreaks.com/topic/170447-preg_replace-confusion/#findComment-899100 Share on other sites More sharing options...
shamuraq Posted August 16, 2009 Author Share Posted August 16, 2009 Your regex does not match against an expression, only a simple pattern, you can use either of these: function br2nl($text) { return preg_replace('/<br\\s*?\/??>/i', '', $text); } br2nl($sub); //Using a function echo preg_replace('/<br\\s*?\/??>/i', '', $sub); //Directly What do u mean my regex doesn't match the expression i thought it clearly state the exact match i want to find and change them specifically. Please do explain. I'm still pretty new to PHP and any explanation that could assist my transition into a better understanding will definitely be of great help. For instance what is regex? I google it but could not understand heads or tails. Apparently there's a lot of problem with regex. Quote Link to comment https://forums.phpfreaks.com/topic/170447-preg_replace-confusion/#findComment-899380 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.