Jump to content

The whole string not being printed


swampone

Recommended Posts

A user submit a comment to my database. If the user comment includes a word that has "" it stops the rest of the comment from being printed. I guess it has to be escaped somehow.


$info = "this is a "testing" comment";
echo $info;

//This will only print:  this is a 
//I need it to print everything: this is a "testing" commentecho $info;

 

Link to comment
https://forums.phpfreaks.com/topic/213103-the-whole-string-not-being-printed/
Share on other sites

A user submit a comment to my database. If the user comment includes a word that has "" it stops the rest of the comment from being printed. I guess it has to be escaped somehow.


$info = "this is a "testing" comment";
echo $info;

//This will only print:  this is a 
//I need it to print everything: this is a "testing" commentecho $info;

 

You can't figure out why?..... just look your $info variable and the what is printing.... what do you think is happening?

swampone,

 

I am still a novice but I think i can solve this for you :)

in php you can have a string with double quotes or with single quotes. And here docs but that's not interesting now.

 

If you start a string with double quotes and want to display double quotes on output you must escape the double quotes. ie

$string = "i am a \"fat\" monkey";

 

If you start a string with single quotes and want to display single quotes on output you must escape the single quotes. ie

$string = 'we are "fat" monkey\'s';

You can't figure out why?..... just look your $info variable and the what is printing.... what do you think is happening?

Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.

Staying true to your signature I see

 

Back on the topic...

Take a look at

$info = "this is a "testing" comment";

 

the quotations in "testing" are closing the original quotations around the string and so only "this is a " is being echoed

 

check out this tutorial

and then look at this

I see that. But that's the way its stored in the database. Im finding that users want to put words in quotes.

 

Back To Topic:

 

If you are talking about storing fields in the database with special characters on them, you should read about

mysql_real_escape_string(),  addslashes(),  and stripslashes() at minimum

 

in a direct string as the one that you showed  the additional " need to be escaped... "this is a \"testing\" .... etc... "

I am still having trouble with this, so let me see if i can be more clear. A user submits a comment through a html form. The comments makes it to the database, no problem, even if a user decide that they want to use double quotes on some of the words in their comment.  The problem occurs when I pull the comment from the database and the user used double quotes in there comment, this is where i get the break in the comment.

$query = "SELECT *  FROM post";
$result = mysql($query);
while($row = mysql_fetch_array($result)){
     $comment = stripslashes($row["comment"]);
echo $comment;
}

 

This works fine if a user submits a comment that doesn't contain the double quotes.

Archived

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

×
×
  • 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.