Double Quotes Within PHP Script Echo

Double quotes within php script echo

You need to escape ", so it won't be interpreted as end of string. Use \ to escape it:

echo "<script>$('#edit_errors').html('<h3><em><font color=\"red\">Please Correct Errors Before Proceeding</font></em></h3>')</script>";

Read more: strings and escape sequences

How to work with double quotes and single quotes in php

You have to escape double quotes if your string is put in double quotes by using backslash \ character. Here is an example:

echo "String with \"double quotes\"";

So in your case it will be:

echo "<img src=\"img_fjords.jpg\" onclick=\"document.getElementById('someID').style.display='block'\" class=\"w3-hover-opacity\"";

Ref: Double quotes within php script echo

How to escape PHP inside the double quotes of html?

As @quentin mentioned, you're trying to echo a php tag, which you cannot. To use a variable inside a string, without messing with quotes, I normally use heredoc, i.e.:

<?php
$value = 100;
echo <<< EOF
<textarea name="" class="widefat" style="width:100%; height: 100px;" value="{$value}"></textarea>
EOF;

How to remove double quotes from an echo statement

//your double quotes come in this line:
$values = '<option value="' . $row['name'] . '">"' . $row['name'] . '"</option>';

//change it to:
$values = '<option value="' . $row['name'] . '">' . $row['name'] . '</option>';

How to output a string with a double quotation mark?

$new_str = str_replace('\'', '"', $web);

You could optional do it by modifying the actual string (note the use of \ to escape the quotation marks):

$web = "...if (url.contains(\".mp4\"))..."

How to include the single and double quotes in char list?

Both of your regexes produce syntax errors in PHP due to there being quotation marks inside of the regex itself.

If you don't want to concatenate the regex into two different parts, your best approach is to escape the quotation marks with backslashes:

$regex = "^[-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]+$";
$regex = '^[-!$%^&*()_+|~=`{}\[\]:";\'<>?,.\/]+$';

Note that the latter is more preferable, as double quotes will parse any variables stored within the string; if there were any text after the $, and a corresponding variable, the variable's content would be injected into the regex:

$sample = 'text';
$regex = "^[-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]+$sample";

echo $regex;
// ^[-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]+text

In addition to this, it's also slightly faster to use single quotes.

Escape double quotes with variable inside HTML echo

Some tips on outputting HTML with PHP:

  1. Use single quotes so that you don't have to escape the double quotes (when using echo),
  2. Use htmlspecialchars() to properly escape any "rogue" values you may have.

Example using echo:

echo '<input type="hidden" name="id" value="', htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'), '" />';

Or printf():

printf('<input type="hidden" name="id" value="%s" />', 
htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8')
);

Or, in HTML mode:

?>
<input type="hidden" name="id" value="<?php echo htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'); ?>" />
<?php

Using multiple quotations in PHP codes

  • If you use single quotes outside, you need to escape all single quotes inside, but can use double quotes and the dollar char without escaping.
  • If you use double quotes outside, you need to escape all double quotes and dollar chars inside, but can use single quotes without escaping.
  • If you use a heredoc string, you need to escape dollar chars but can use both single and double quotes without escaping.
  • If you use a nowdoc string, you do not need to escape anything unless you have FOO; in the string at the beginning of a new line.

So the solution is to use a nowdoc string:

$message = <<<'EOF'
your stuff with " or ' or $ here!
EOF;


Related Topics



Leave a reply



Submit