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

Link to comment
Share on other sites

echo htmlentities($word);

 

That wouldn't work...as it only converts characters to their HTML counterpart, on second thought I assume you're talking to denno020 about a solution to his issue with tag interpretation...

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

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.