Escape double quotes for HTML attributes output by PHP

Often when writing PHP, I will output some HTML like this -

echo "<a href="../" title="link title">".$link_text."</a>";

Obviously this will not be parsed as I need to avoid double quotes in the attributes of the element <a>. Is there a regex that would quickly do this instead of manually adding a backslash?

One more thing: the regex should not escape double quotes outside the tag (for example, where I added the variable $link_text.

Any ideas?

+3
source share
6 answers

Instead, use only quotation marks:

echo '<a href="../" title="link title">' . $link_text . '</a>';
+13
source

, ( ):

  • echo '<a href="../">' . $link_text. '</a>';
    
  • echo "<a href='../'>$link_text</a>";
    
  • Sprintf

    echo sprintf('<a href="../">%s</a>', $link_text);
    
  • HEREDOC

    echo <<<EOF
    <a href="../">$link_text</a>
    EOF;
    
  • , smarty

  • PHP-:

    ?><a href="../"><?php echo $link_text ?></a><?php // other code...
    

, htmlspecialchars() on $link_text, XSS.

+8

( ..)

echo <<<EOT
<a href="../" title="link title">$link_text</a>
EOT;
+5

, .

PHP:

<a href="../" title="link title"><?php echo $link_text; ?></a>
+3
0

,

http://www.example.com/.../Learning-Tutorials/ACTIVE-USER-ACCOUNT/verify.php?email='.$email.'&hash='.$hash.'

"<a href="//www.example.com/.../Learning-Tutorials/ACTIVE-USER-ACCOUNT/verify.php?email="$email&hash=$hash>Click Here to Active</a>"

try it.

0
source

Source: https://habr.com/ru/post/1712082/


All Articles