toulee_moua Posted January 6, 2011 Share Posted January 6, 2011 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 More sharing options...
denno020 Posted January 6, 2011 Share Posted January 6, 2011 Try using double qutoes.. You might need to be escaping the symbols if you use single quotes (putting a \ directly before it). Just thinking out loud... Denno Link to comment https://forums.phpfreaks.com/topic/223576-replacing-and-with-and/#findComment-1155690 Share on other sites More sharing options...
AbraCadaver Posted January 6, 2011 Share Posted January 6, 2011 This works great for me: $word = "(test)"; $word = str_replace(array('(', ')'), array('<', '>'), $word); Link to comment https://forums.phpfreaks.com/topic/223576-replacing-and-with-and/#findComment-1155692 Share on other sites More sharing options...
denno020 Posted January 6, 2011 Share Posted January 6, 2011 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 https://forums.phpfreaks.com/topic/223576-replacing-and-with-and/#findComment-1155693 Share on other sites More sharing options...
Adam Posted January 6, 2011 Share Posted January 6, 2011 echo htmlentities($word); Link to comment https://forums.phpfreaks.com/topic/223576-replacing-and-with-and/#findComment-1155701 Share on other sites More sharing options...
Zurev Posted January 6, 2011 Share Posted January 6, 2011 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 https://forums.phpfreaks.com/topic/223576-replacing-and-with-and/#findComment-1155705 Share on other sites More sharing options...
Adam Posted January 6, 2011 Share Posted January 6, 2011 You were right with your second thought. Link to comment https://forums.phpfreaks.com/topic/223576-replacing-and-with-and/#findComment-1155719 Share on other sites More sharing options...
toulee_moua Posted January 6, 2011 Author Share Posted January 6, 2011 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 https://forums.phpfreaks.com/topic/223576-replacing-and-with-and/#findComment-1155730 Share on other sites More sharing options...
PFMaBiSmAd Posted January 6, 2011 Share Posted January 6, 2011 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 https://forums.phpfreaks.com/topic/223576-replacing-and-with-and/#findComment-1155732 Share on other sites More sharing options...
toulee_moua Posted January 6, 2011 Author Share Posted January 6, 2011 What i'm trying to do is replace all "(" to "<" and ")" to ">", like "(TEST)" to "<TEST>", then display them as text. Link to comment https://forums.phpfreaks.com/topic/223576-replacing-and-with-and/#findComment-1155738 Share on other sites More sharing options...
denno020 Posted January 6, 2011 Share Posted January 6, 2011 $word = str_replace(array('(', ')'), array('<', '>'), $word); reference: http://www.w3schools.com/html/html_entities.asp Denno Link to comment https://forums.phpfreaks.com/topic/223576-replacing-and-with-and/#findComment-1155873 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.