[英]Quotes within quotes - as a string?
我有一个PHP字符串:
$str = '';
我需要在其中添加一些HTML,但是HTML的“引号内有引号”。 如何处理这种情况?
HTML是:
data-setup="{'example_option':true}"
要处理引号内的引号,您需要像这样对它们进行转义:
echo "<a href=\"somelink.html\">Link</a>";
简单的说:
\" tells php to simply echo out the " as a quote instead of handling it as a part of the code.
您的字符串:
$string = "data-setup=\"{'example_option':true}\""
echo $string;
// Output:
data-setup="{'example_option':true}"
您也可以使用HEREDOC
<?php
$html = <<<EOD
data-setup="{'example_option':true}"
EOD;
echo $html;
看起来您想通过data-attribute
PHP中的一些数据传递给视图。 我建议您使用json_encode
并将您的数据构建为普通的PHP数组,而不用担心转义引号或其他令人讨厌的东西。
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<?php
$data = array(
'example_option' => true,
'some_html' => '<b>"Hello & good day!"</b>',
);
// avoid escaping from the JS parser
$str = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
?>
<a data-setup='<?php echo $str; ?>' href="#">This link has awesome data</a>
</body>
</html>
然后,您可以非常轻松地访问数据属性(在此我确实做了一个大的假设,即您正在使用jQuery)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.