Jump to content

Archived

This topic is now archived and is closed to further replies.

Allen4172

Eliminating single apostraphe in echo statements?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
You could use the htmlentities() function (http://www.php.net/htmlentities)
[code]<?php
echo htmlentities("Text won't always be nice");
?>[/code]

Ken

Share this post


Link to post
Share on other sites
I can't use double quotes since there are more double quotes than single quotes.  For example this wouldn't work:

[code]
<?php
echo htmlentities('Text won't always be nice');
?>
[/code]

It gives an error of unexpected T_STRING.

Share this post


Link to post
Share on other sites
[code]
<?php
echo addslashes('Text hate's single quotes');
?>
[/code]

Gives an T_LNUMBER errror when I attempt that.

Share this post


Link to post
Share on other sites
You can also create a function-
[code]<?php
safe_echo($text){ //$text is the text to be printed
echo(str_replace("'", "\'", $text));
}
?>[/code]

Orio.

Share this post


Link to post
Share on other sites
[quote]
You can also create a function-
[code]
<?php
safe_echo($text){ //$text is the text to be printed
echo(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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.