Jump to content

Eliminating single apostraphe in echo statements?


Allen4172

Recommended Posts

[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?
Link to comment
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
Link to comment
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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

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.