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

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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... "

Link to comment
Share on other sites

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.

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.