Cyto Posted August 8, 2011 Share Posted August 8, 2011 Hi, I want to add a quote to strings if needed. Example: Papa s Home, must be Papa's Home. Is this possible? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/ Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 $string = 'Papa\'s'; Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254576 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 escape the quotes. $string = "Papa\'s Home"; in this situation, you actually wouldn't have to, because I used double quotes on the outside, and a single quote inside, but if you're planning on putting this into a database you'll definitely need to escape your strings properly. look up mysql_real_escape_string, addslashes and stuff like that. good luck Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254577 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 @AyKay47 lol, it's like I'm following you! Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254578 Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 escape the quotes. $string = "Papa\'s Home"; if you test this, it actually won't work while the string is encased in double quotes...this will only work if it is encased in single quotes...double quotes are mainly used to escape special characters..however characters that do not need to be escaped will display the backslash as well... @AyKay47 lol, it's like I'm following you! Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254584 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 of course it will work, except you need to add stripslashes when echoing it: echo stripslashes($string); Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254588 Share on other sites More sharing options...
Cyto Posted August 8, 2011 Author Share Posted August 8, 2011 escape the quotes. $string = "Papa\'s Home"; in this situation, you actually wouldn't have to, because I used double quotes on the outside, and a single quote inside, but if you're planning on putting this into a database you'll definitely need to escape your strings properly. look up mysql_real_escape_string, addslashes and stuff like that. good luck Another problem that I'm having: I want to get ($_GET) the name like this: Papa's, so i can get the result from the database.. The url: http://www.link.com/papa-s-home/ but i need to have papa's home with a quote to get a match from the database. So i need to add a quote at the empty spot Papa s, but how can i do this. My part of the code: $getname = $_GET["name"]; $cleanname = array("-","/"); $name=mysql_real_escape_string(str_replace($cleanname," ",$getname)); I hope you'll understand it, sorry for my english Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254591 Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 okay so you'll use something like $getname = $_GET["name"]; $cleanname = array("-","/"); $name = str_replace($cleanname,"'",$getname); $name should output papa's of course it will work, except you need to add stripslashes when echoing it: echo stripslashes($string); ew.. Note: you can actually write it like this as well... $string = "Papa's"; Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254596 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 @Cyto check out urlencode too. I'm guessing it will come in handy if you're using variables with values like that. Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254601 Share on other sites More sharing options...
Cyto Posted August 8, 2011 Author Share Posted August 8, 2011 okay so you'll use something like $getname = $_GET["name"]; $cleanname = array("-","/"); $name = str_replace($cleanname,"'",$getname); $name should output papa's This works if the result were all with quotes, but it isn't. Then the names without a quote wouldn't work. I'm already adding the names with stripslashes. The name is already Papa's Home in the database. But I when i go to this link http://www.link.com/papa-s-home/, it querys papa s home. I need to add a quote somehow to strings like papa s, mama s etc. To get a result from the database. Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254606 Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 then you'll want.. $getname = $_GET["name"]; $name = str_replace(" "."'",$getname); will replace the space in the string with an apostrophe Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254609 Share on other sites More sharing options...
Cyto Posted August 8, 2011 Author Share Posted August 8, 2011 then you'll want.. $getname = $_GET["name"]; $name = str_replace(" "."'",$getname); will replace the space in the string with an apostrophe I know, but doesnt it replace the space too, it will output Papa's'Home instead of Papa's Home? Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254615 Share on other sites More sharing options...
WebStyles Posted August 9, 2011 Share Posted August 9, 2011 you could do this: $name = str_replace(" s","'s",$_GET['name']); so only spaces followed by an 's' get replaced. Not brilliant, but it will work in your case. Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254749 Share on other sites More sharing options...
phpSensei Posted August 9, 2011 Share Posted August 9, 2011 of course it will work, except you need to add stripslashes when echoing it: echo stripslashes($string); Or just use the proper quotations? Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254770 Share on other sites More sharing options...
Cyto Posted August 9, 2011 Author Share Posted August 9, 2011 Thx WebStyles, AyKay47, it worked. If someone has the same problem, dont forget to add a slash: \'s. It wil read the result with a quote from the database. Quote Link to comment https://forums.phpfreaks.com/topic/244269-add-a-quote-to-a-string/#findComment-1254935 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.