Allen4172 Posted August 6, 2006 Share Posted August 6, 2006 I have something like the following:[code]echo 'Text wont' always be nice';[/code]What's the best way to automatically (not manually putting a slash) ensure that the single quote in the echo statement won't break it? Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/ Share on other sites More sharing options...
Aurorius Posted August 6, 2006 Share Posted August 6, 2006 How about this ? [code]echo "Text wont' always be nice";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70058 Share on other sites More sharing options...
kenrbnsn Posted August 6, 2006 Share Posted August 6, 2006 You could use the htmlentities() function (http://www.php.net/htmlentities)[code]<?phpecho htmlentities("Text won't always be nice");?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70195 Share on other sites More sharing options...
Allen4172 Posted August 6, 2006 Author Share Posted August 6, 2006 I can't use double quotes since there are more double quotes than single quotes. For example this wouldn't work:[code]<?phpecho htmlentities('Text won't always be nice');?>[/code]It gives an error of unexpected T_STRING. Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70233 Share on other sites More sharing options...
king arthur Posted August 6, 2006 Share Posted August 6, 2006 Where is the text coming from? Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70237 Share on other sites More sharing options...
kenrbnsn Posted August 6, 2006 Share Posted August 6, 2006 Use the addslashes() function.Ken Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70247 Share on other sites More sharing options...
Allen4172 Posted August 6, 2006 Author Share Posted August 6, 2006 [code]<?phpecho addslashes('Text hate's single quotes');?> [/code]Gives an T_LNUMBER errror when I attempt that. Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70252 Share on other sites More sharing options...
Orio Posted August 6, 2006 Share Posted August 6, 2006 You can also create a function-[code]<?phpsafe_echo($text){ //$text is the text to be printedecho(str_replace("'", "\'", $text));}?>[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70253 Share on other sites More sharing options...
Allen4172 Posted August 6, 2006 Author Share Posted August 6, 2006 [quote]You can also create a function-[code]<?phpsafe_echo($text){ //$text is the text to be printedecho(str_replace("'", "\'", $text));}?>[/code][/quote]That seems like the most promising so far. But if I declare $text = 'Single quote's suck'; above, it'll give the error. How would I go about declaring $text without breaking the code? Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70259 Share on other sites More sharing options...
kenrbnsn Posted August 6, 2006 Share Posted August 6, 2006 If you have a single quote in a string you need to either use double quotes to define the string or manully put in the backslashes. You can also concatenate strings, Here is an example of doing this many different ways.[code]<?php$qte = "'";$dqte = '"';$str = array();$str[] = 'This string contains a single quote ...' . $qte . ' ...';$str[] = "So does this string [']";$str[] = "This string contains both single(') and double(\") quotes";$str[] = 'As does this one [\'] ["]';$str[] = 'And this --- ' . $qte . ' --- ' . $dqte . '!';$str[] = 'He said "that' . "'" . 's the wrong way of doing things, ' . "here's " . 'the right way"';foreach ($str as $line) echo $line."<br>\n";?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70262 Share on other sites More sharing options...
Allen4172 Posted August 6, 2006 Author Share Posted August 6, 2006 That's the problem though. I'm looking to try and find away to automize the text not to give an error and auto add a backslash.An example real world text would be this: <p style="margin:0cm 0cm 0pt">by completing a registration form and returning it on Friday you'll be able to avoid late fees.As you can see the real world text has BOTH " and ' in it. Trying to explain to people who have used the web very little in the past, that you must always use a backslash before a single quote is the equiv to banging your head against a wall. Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70266 Share on other sites More sharing options...
kenrbnsn Posted August 6, 2006 Share Posted August 6, 2006 Where is this text being inputted? Are you accepting it in a form? We need to know more about what you're trying to do before we can give you a meaningful answer.If it is coming in via a form, let them type anything they want. You deal with it in your code.Ken Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70269 Share on other sites More sharing options...
Allen4172 Posted August 6, 2006 Author Share Posted August 6, 2006 Its a closed source CMS that is being entered via fields similar to forms. So I have no control over what they enter and can not modify the existing program since its closed source (my script deals with the inputs after the program is done with it). I've researched google and the php manual for answers for a couple weeks and couldn't find anything meaningful that would help. I'm sure I'm not the first person with this problem, but just can't seem to find the answer.In the program, for example, I'm getting a field called $text$. So right now I declar it via $text = '$text$' since there is a chance a single quote won't be in there. However there will ALWAYS be a double quote in there from the WYSIWYG editor in the CMS. Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70279 Share on other sites More sharing options...
kenrbnsn Posted August 6, 2006 Share Posted August 6, 2006 Using [code]<?php $text = '$text$'; ?>[/code]will only get you a variable with the string [b]$text$[/b] in it.Have you tried just echoing them as is? Don't try to be fancy -- KIS -- Keep It Simple.Now that you've told us where the values are coming from, tell us (and show some code) where you're having problems.Ken Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70285 Share on other sites More sharing options...
Allen4172 Posted August 6, 2006 Author Share Posted August 6, 2006 Your right. In this case I ONLY want to get that $text$ variable out.Problem lies in that statement:[code]<?php $text = '$text$'; ?>[/code]If anywhere in $text$ it has a ' then it will break that echo statement and give an error. So thus I'm searching for a way to somehow auto add a backslah in that string to avoid getting parse errors. Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70289 Share on other sites More sharing options...
Allen4172 Posted August 7, 2006 Author Share Posted August 7, 2006 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/16686-eliminating-single-apostraphe-in-echo-statements/#findComment-70849 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.