Jump to content

Single Quotes Giving Me Grief


Johnny2

Recommended Posts

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 by Johnny2
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...