Jump to content

[SOLVED] Trying to replace < and > with < and >


Potatis

Recommended Posts

Hi, I am trying to come up with a function that will replace < and > with < and >

 

The data contains html and php code and comes from a form to a processing page as $_POST['content'];

 

This is what I was trying:

 

<?php

function replace_bracket($var) {
        $search = array(
                '<',
                '>',
               
                );

        $replace = array(
                '<',
                '>',
               );

        $var = preg_replace($search, $replace, $var);
        return $var;
}


$content = $_POST['content'];

echo replace_bracket($content);

?>

 

My error message is: Warning: preg_replace() [function.preg-replace]: No ending matching delimiter '>' found in C:\wamp\www\snippets\snippet_process.php on line 15

 

and line 15 is: $var = preg_replace($search, $replace, $var);

 

I'm new to preg_replace, I have no idea what I am doing.  :-[ I hope I can get some help  ???

Link to comment
Share on other sites

In case you were wondering about the error, preg_replace (and pretty much every other regex function in existence) expects delimiters to be put around the pattern, so that the engine knows what the start and end of the pattern is. 

 

$search = array(
                '~<~',
                '~>~',

 

There are several different chars that can be used. Some of the more popular ones are /../ !..! #..# ~...~

You can even do <...> (notice it's open/close not open/open, in this case). Which is where your specific error comes into play. It saw the < and interpreted it as your opening delimiter instead of what you were wanting to match, saw no pattern after it (which is fine, you don't *need* a pattern but what's the point of regex without one...), and no closing delimiter of > to match the initial <.

 

 

Link to comment
Share on other sites

Thanks very much for that, because although htmlentities did do the job, it also replaced everything else to make it html friendly, and this actually made things worse for what I was doing. I really only needed to replace the < with < and so I resorted to using Dreamweaver's find and replace thing, not that I use Dreamweaver for anything else anymore.

 

I am learning as much as I can about preg_replace, and although there are many samples around, there is little information to go with it that is in plain English. I mostly am left to look at the finished code and work out what happened. My interest in preg_replace keeps me interested in wanting to become proficient in the use of this function, so I keep trying to understand it. :)

 

 

Link to comment
Share on other sites

Do you mean something like this?

 

function replace_brackets($var) {
$replace = array('<' => '<', '>' => '>');
return strtr($var, $replace);
}

$str = 'This is text <someTag>More text</sometag>';
echo replace_brackets($str);

 

Output (on screen):

This is text <someTag>More text</sometag>

 

Output (via right-click view source):

This is text <someTag>More text</sometag>

Link to comment
Share on other sites

Do you mean something like this?

 

function replace_brackets($var) {
$replace = array('<' => '<', '>' => '>');
return strtr($var, $replace);
}

$str = 'This is text <someTag>More text</sometag>';
echo replace_brackets($str);

 

Output (on screen):

This is text <someTag>More text</sometag>

 

Output (via right-click view source):

This is text <someTag>More text</sometag>

 

Worked perfectly, thanks! I appreciate it very much, nrg_alpha! :)

 

Thanks to all who replied!

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.