将php函数生成的JavaScript代码放在php变量中,然后在html页面上显示,而不执行
我有以下php函数将php函数生成的JavaScript代码放在php变量中,然后在html页面上显示,而不执行,javascript,php,html,Javascript,Php,Html,我有以下php函数 <?php function searchbox_markup(){ $baseUrl = "example.com"; ?> <script type="application/ld+json"> { "@context": "http://schema.org", "@type": "WebSite", "url": "<?php echo $baseUrl; ?>", "potentialAction"
<?php function searchbox_markup(){
$baseUrl = "example.com"; ?>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "<?php echo $baseUrl; ?>",
"potentialAction": {
"@type": "SearchAction",
"target": "<?php echo $baseUrl . 'index.php?page=search&sPattern={search_term_string}'; ?>",
"query-input": "required name=search_term_string"
}
}
</script>
<?php } ?>
{
“@context”:”http://schema.org",
“@type”:“网站”,
“url”:“,
“潜在作用”:{
“@type”:“SearchAction”,
“目标”:“,
“查询输入”:“所需名称=搜索\术语\字符串”
}
}
我想在我的页面上显示上述函数的输出,而不执行其中包含的JavaScript代码,我尝试了以下操作,但它没有在html中显示JavaScript代码
<?php
$snippet = '"' . searchbox_markup() . '"';
$htmlSnippet = "<pre>".htmlspecialchars($snippet)."</pre>";
echo $htmlSnippet;
?>
使用htmlspecialchars:
<xmp>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://example.com/",
"potentialAction": {
"@type": "SearchAction",
"target": "http://example.com/index.php?page=search&sPattern={search_term_string}",
"query-input": "required name=search_term_string"
}
}
</script>
</xmp>
$jsCode='1
{
“@context”:”http://schema.org",
“@type”:“网站”,
“url”:”http://example.com/",
“潜在作用”:{
“@type”:“SearchAction”,
“目标”:http://example.com/index.php?page=search&sPattern={search_term_string}”,
“查询输入”:“所需名称=搜索\术语\字符串”
}
}
';
$jsToDisplay=htmlspecialchars($jsCode);
使用htmlspecialchars:
<xmp>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://example.com/",
"potentialAction": {
"@type": "SearchAction",
"target": "http://example.com/index.php?page=search&sPattern={search_term_string}",
"query-input": "required name=search_term_string"
}
}
</script>
</xmp>
$jsCode='1
{
“@context”:”http://schema.org",
“@type”:“网站”,
“url”:”http://example.com/",
“潜在作用”:{
“@type”:“SearchAction”,
“目标”:http://example.com/index.php?page=search&sPattern={search_term_string}”,
“查询输入”:“所需名称=搜索\术语\字符串”
}
}
';
$jsToDisplay=htmlspecialchars($jsCode);
您可以使用
标记
在HTML中
$var='<xmp>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://example.com/",
"potentialAction": {
"@type": "SearchAction",
"target": "http://example.com/index.php?page=search&sPattern={search_term_string}",
"query-input": "required name=search_term_string"
}
}
</script>
</xmp>';
{
“@context”:”http://schema.org",
“@type”:“网站”,
“url”:”http://example.com/",
“潜在作用”:{
“@type”:“SearchAction”,
“目标”:http://example.com/index.php?page=search&sPattern={search_term_string}”,
“查询输入”:“所需名称=搜索\术语\字符串”
}
}
正如您所描述的,您希望将其保存在php变量so中
在php中
$snippet = "<script type='application/ld+json'>
{
'@context': 'http://schema.org',
'@type': 'WebSite',
'url': 'http://example.com/',
'potentialAction': {
'@type': 'SearchAction',
'target': 'http://example.com/index.php?page=search&sPattern={search_term_string}',
'query-input': 'required name=search_term_string'
}
}
</script>";
$htmlSnippet = "<pre>".htmlspecialchars($snippet)."</pre>";
$var='1
{
“@context”:”http://schema.org",
“@type”:“网站”,
“url”:”http://example.com/",
“潜在作用”:{
“@type”:“SearchAction”,
“目标”:http://example.com/index.php?page=search&sPattern={search_term_string}”,
“查询输入”:“所需名称=搜索\术语\字符串”
}
}
';
您可以使用
标记
在HTML中
$var='<xmp>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://example.com/",
"potentialAction": {
"@type": "SearchAction",
"target": "http://example.com/index.php?page=search&sPattern={search_term_string}",
"query-input": "required name=search_term_string"
}
}
</script>
</xmp>';
{
“@context”:”http://schema.org",
“@type”:“网站”,
“url”:”http://example.com/",
“潜在作用”:{
“@type”:“SearchAction”,
“目标”:http://example.com/index.php?page=search&sPattern={search_term_string}”,
“查询输入”:“所需名称=搜索\术语\字符串”
}
}
正如您所描述的,您希望将其保存在php变量so中
在php中
$snippet = "<script type='application/ld+json'>
{
'@context': 'http://schema.org',
'@type': 'WebSite',
'url': 'http://example.com/',
'potentialAction': {
'@type': 'SearchAction',
'target': 'http://example.com/index.php?page=search&sPattern={search_term_string}',
'query-input': 'required name=search_term_string'
}
}
</script>";
$htmlSnippet = "<pre>".htmlspecialchars($snippet)."</pre>";
$var='1
{
“@context”:”http://schema.org",
“@type”:“网站”,
“url”:”http://example.com/",
“潜在作用”:{
“@type”:“SearchAction”,
“目标”:http://example.com/index.php?page=search&sPattern={search_term_string}”,
“查询输入”:“所需名称=搜索\术语\字符串”
}
}
';
如果您试图在页面上打印Javascript代码,则需要将所有HTML字符转换为其HTML符号等价物。要做到这一点,我们可以使用
如果希望结构保持不变,使其看起来像粘贴到问题中的代码,则需要将其包装在相关的HTML标记中。在这种情况下,那就是
$htmlSnippet
PHP变量$htmlSnippet
变量,并最终将问题中的输出打印到页面上,而不将其解释为HTML
笔记:
-
<> LI>您可能想考虑使用HTML标记,因为那时您明确地说明元素中包含的是代码,而不是其他任何东西。默认情况下,它还将以不同的字体显示代码
- 你可以使用,而不是取决于你的情况。有关更多信息,请查看
更新: 更新问题后,您似乎希望问题中的函数返回此值。您需要调整函数以将该值作为字符串返回。类似的方法可能会奏效:
函数搜索框\u标记(){
$baseUrl=“example.com”;
$snippet=”
{
“@context”:”http://schema.org',
“@type”:“网站”,
“url”:“$baseUrl.”,
“潜在作用”:{
“@type”:“SearchAction”,
'target':'“$baseUrl.”index.php?page=search&sPattern={search\u term\u string},
“查询输入”:“所需名称=搜索\术语\字符串”
}
}
";
返回$snippet;
}
对我来说,这看起来很混乱。几乎可以肯定,有一种更好的方法可以解决您试图解决的问题。如果您试图将Javascript代码打印到页面上,则需要将所有HTML字符转换为其HTML符号等价物。要做到这一点,我们可以使用 如果希望结构保持不变,使其看起来像粘贴到问题中的代码,则需要将其包装在相关的HTML标记中。在这种情况下,那就是
$htmlSnippet
PHP变量$htmlSnippet
变量,并最终将问题中的输出打印到页面上,而不将其解释为HTML
笔记:
-
<> LI>您可能想考虑使用HTML标记,因为那时您明确地说明元素中包含的是代码,而不是其他任何东西。默认情况下,它还将以不同的字体显示代码
- 你可以使用,而不是取决于你的情况。有关更多信息,请查看
更新: 更新问题后,您似乎希望返回此值