Jump to content


Photo

Eliminating single apostraphe in echo statements?


  • Please log in to reply
15 replies to this topic

#1 Allen4172

Allen4172
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 06 August 2006 - 05:50 AM

I have something like the following:

echo 'Text wont' always be nice';

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?

#2 Aurorius

Aurorius
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationMalaysia

Posted 06 August 2006 - 05:56 AM

How about this ?

echo "Text wont' always be nice";


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 August 2006 - 01:38 PM

You could use the htmlentities() function (http://www.php.net/htmlentities)
<?php
echo htmlentities("Text won't always be nice");
?>

Ken

#4 Allen4172

Allen4172
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 06 August 2006 - 03:00 PM

I can't use double quotes since there are more double quotes than single quotes.  For example this wouldn't work:

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

It gives an error of unexpected T_STRING.

#5 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 06 August 2006 - 03:02 PM

Where is the text coming from?
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 August 2006 - 03:35 PM

Use the addslashes() function.

Ken

#7 Allen4172

Allen4172
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 06 August 2006 - 03:42 PM

<?php
echo addslashes('Text hate's single quotes');
?> 

Gives an T_LNUMBER errror when I attempt that.

#8 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 06 August 2006 - 03:43 PM

You can also create a function-
<?php
safe_echo($text){ //$text is the text to be printed
echo(str_replace("'", "\'", $text));
}
?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#9 Allen4172

Allen4172
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 06 August 2006 - 03:56 PM

You can also create a function-

<?php
safe_echo($text){ //$text is the text to be printed
echo(str_replace("'", "\'", $text));
}
?>


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?

#10 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 August 2006 - 04:08 PM

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.

<?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";
?>

Ken

#11 Allen4172

Allen4172
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 06 August 2006 - 04:20 PM

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.



#12 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 August 2006 - 04:24 PM

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

#13 Allen4172

Allen4172
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 06 August 2006 - 04:48 PM

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.

#14 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 August 2006 - 04:59 PM

Using
<?php $text = '$text$'; ?>
will only get you a variable with the string $text$ 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

#15 Allen4172

Allen4172
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 06 August 2006 - 05:16 PM

Your right.  In this case I ONLY want to get that $text$ variable out.

Problem lies in that statement:

<?php $text = '$text$'; ?>

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.

#16 Allen4172

Allen4172
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 07 August 2006 - 10:06 PM

Any ideas?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users