Jump to content

preg_replace confusion


shamuraq

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/170447-preg_replace-confusion/
Share on other sites

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

 

 

 

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.

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.