Jump to content

Replacing ( and ) with < and >


toulee_moua

Recommended Posts

Please help.  I am trying to replace parenthesises () in a string with <>.  I tried preg_replace and str_replace, but was not success.

 

Here is what I have for str_replace:

$word = "(test)";

 

$word = str_replace('(', '<', $word);

 

$word = str_replace(array('(', ')'), array('<','>'), $word);

 

 

Link to comment
https://forums.phpfreaks.com/topic/223576-replacing-and-with-and/
Share on other sites

This works great for me:

 

$word = "(test)";
$word = str_replace(array('(', ')'), array('<', '>'), $word);

 

I just tested it on my testing server and it worked fine too... It won't display on a page though, only (test) will display as the <test> is interpreted as a tag, so it isn't displayed as text. You can see it if you look at the page source though..

 

Denno

Thank folks, for responding.  Here is what I get:

 

$word = "(test)";

$word = str_replace(array("(", ")"), array("<", ">"), $word);

echo $word;                          //OUTPUT: 

echo htmlentities($word);    // OUTPUT: <test>

 

 

Is there a way to treat the <> normal characters?

Define: normal characters?

 

< and > have significance in HTML since they define the start and end of HTML tags. Anything inside of < and > characters is a HTML tag and is not displayed in the browser.

 

Perhaps if you define what it is you are trying to accomplish?

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.