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

 

 

 

Link to comment
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

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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