LLeoun Posted May 28, 2009 Share Posted May 28, 2009 Hi all, I'm trying to do something that seems easy but I'm stuck, please help! What I need is to escape ' in my php string so my javascript won't break. The code below works but I cannot edit all the information I have and change ' into \' ($str is just an example the real thing comes from webservice and it's huge) So working with this code, using $str = "Is your name O'reilly?"; without the \ how can I escape the ' ? Thanks a ton !!! <?php $str = "Is your name O\'reilly?"; echo" <script> alert('";echo $str; echo"'); </script> "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/160000-solved-escaping-quotes/ Share on other sites More sharing options...
MadTechie Posted May 28, 2009 Share Posted May 28, 2009 in that case, your need to escape the quotes Options are as follows $text = str_replace("'", "\'", $text); $text = addslashes($text); $text = htmlspecialchars($text, ENT_QUOTES); $text = htmlentities($text, ENT_QUOTES); Quote Link to comment https://forums.phpfreaks.com/topic/160000-solved-escaping-quotes/#findComment-843997 Share on other sites More sharing options...
LLeoun Posted May 28, 2009 Author Share Posted May 28, 2009 Thanks for answering MadTechie. The only option that seems to work is: $text = htmlspecialchars($text, ENT_QUOTES); But in the alert I get: Is your name O'reilly? Instead of: Is your name O'reilly? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/160000-solved-escaping-quotes/#findComment-844006 Share on other sites More sharing options...
redarrow Posted May 28, 2009 Share Posted May 28, 2009 look up the special character needed then str_replace() it http://www.degraeve.com/reference/specialcharacters.php Quote Link to comment https://forums.phpfreaks.com/topic/160000-solved-escaping-quotes/#findComment-844015 Share on other sites More sharing options...
MadTechie Posted May 28, 2009 Share Posted May 28, 2009 try this $text = htmlspecialchars(str_replace("'", "\'", $text)); basically it converts ' to \' and then encodes the htm (if needed but doesn't handle the single quotes) Quote Link to comment https://forums.phpfreaks.com/topic/160000-solved-escaping-quotes/#findComment-844017 Share on other sites More sharing options...
LLeoun Posted May 28, 2009 Author Share Posted May 28, 2009 Whoah, thanks a ton, fixed!! Quote Link to comment https://forums.phpfreaks.com/topic/160000-solved-escaping-quotes/#findComment-844021 Share on other sites More sharing options...
MadTechie Posted May 28, 2009 Share Posted May 28, 2009 as a note the alert showed ' but if you viewed on a page it will appear as ' Topic solved ? if so please click solved Quote Link to comment https://forums.phpfreaks.com/topic/160000-solved-escaping-quotes/#findComment-844025 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.