Johnny2 Posted March 24, 2013 Report Share Posted March 24, 2013 (edited) So, let's say you have to work with a variable that has a single quote in it like: $name = "Johnny's Thing"; I figured out how to work with it in mysql by using mysql_real_escape_string(). But how do I work with it in this instance?: $list .= "<a href='editStuff.php?name=$name'>Click Here</a>"; The single quote inside the variable is not allowing it to work correctly. I've tried addslashes(), but that doesn't seem to help. Any suggestions? Edited March 24, 2013 by Johnny2 Quote Link to comment Share on other sites More sharing options...
falkencreative Posted March 24, 2013 Report Share Posted March 24, 2013 How are you trying to use it? Are you getting any specific error messages? I don't really get any specific issues with your sample code. For example, this works the way I'd expect: <?php $name = 'test'; $list = "<a href='editStuff.php?name=$name'>Click Here</a>"; echo $list; Quote Link to comment Share on other sites More sharing options...
Johnny2 Posted March 24, 2013 Author Report Share Posted March 24, 2013 How are you trying to use it? Are you getting any specific error messages? I don't really get any specific issues with your sample code. For example, this works the way I'd expect: <?php $name = 'test'; $list = "<a href='editStuff.php?name=$name'>Click Here</a>"; echo $list; Yes, but the example you have shown assigns the variable $name with 'test'. You need to store a variable that contains an apostrophe to see what I mean. Such as $name = "Johnny's". Quote Link to comment Share on other sites More sharing options...
falkencreative Posted March 24, 2013 Report Share Posted March 24, 2013 Sorry, guess I misunderstood. To be honest, the more I think about this, the more I'd suggest avoiding the problem entirely -- why do you need quotes in a URL anyway? I'd really just suggest sticking primarily to alphanumeric characters only in URLs -- standard letters and numbers and dashes. You could use this to strip quotes from a variable: $name = "test'ing"; $name = str_replace(array('"', "'"), '', $name); Alternately, if you really need that apostrophe, you can encode it into the URL using "%27" (http://www.w3schools.com/TAGS/ref_urlencode.asp) $name = "test'ing"; $name = str_replace("'", '%27', $name); Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.