I have following php function
<?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 } ?>
I want to display the output of above function on my page without executing JavaScript code contained in it, I tried the following but it do not display JavaScript code in html.
<?php
$snippet = '"' . searchbox_markup() . '"';
$htmlSnippet = "<pre>".htmlspecialchars($snippet)."</pre>";
echo $htmlSnippet;
?>
-
You do not store javascript value in php variable because of first run php script and then run javascript.Nikhil Vaghela– Nikhil Vaghela2016年08月23日 11:08:04 +00:00Commented Aug 23, 2016 at 11:08
-
What do you mean by '... and display on my page without executing'?Henders– Henders2016年08月23日 11:08:24 +00:00Commented Aug 23, 2016 at 11:08
-
1Possible duplicate of How to pass variables and data from PHP to JavaScript?Nikhil Vaghela– Nikhil Vaghela2016年08月23日 11:10:39 +00:00Commented Aug 23, 2016 at 11:10
-
I want to show the code part of my question on html page.Syed– Syed2016年08月23日 11:11:12 +00:00Commented Aug 23, 2016 at 11:11
-
@Nikhil Vaghla It is not duplicate, main part of my question is how to show JavaScript code in html.Syed– Syed2016年08月23日 11:15:27 +00:00Commented Aug 23, 2016 at 11:15
4 Answers 4
If you are attempting to print Javascript code to the page you'll want to convert all of the characters which are HTML characters to be their HTML Symbol equivalents. To do this we can use htmlspecialchars().
If you wanted the structure to persist, so that it looks like the code you pasted into the question you'll need to wrap it in the relevant HTML tag. In this case, that would be <pre>. Previously, you could have also used <xmp> but this is now deprecated and its use is discouraged.
Putting all of this together:
$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>";
This code does the following:
- Takes the code you want to use and assigns it to a variable (
$snippet) as a string (taking care to ensure the quotes do not break the string enclosure). - Converts the
$snippetusinghtmlspecialchars() - Concatenates the HTML tags
<pre>and</pre>around the output ofhtmlentities(). - Assign all of this to our
$htmlSnippetPHP variable.
We could then echo this $htmlSnippet variable and end up with the output in your question printed to the page without it being interpreted as HTML.
Notes:
- You might want to consider using the
<code></code>HTML tag instead because then you are explicitly stating that what is contained in that element is code and not anything else. It will also, by default, display the code in a different font. - You could use
htmlentities()instead ofhtmlspecialchars()depending on your circumstance. For more information check out this answer
Update:
After the update to your question, you appear to want to have this value returned by the function in your question. You would need to adjust your function to return this value as a string. Something like this would probably work:
function searchbox_markup(){
$baseUrl = "example.com";
$snippet = "<script type='application/ld+json'>
{
'@context': 'http://schema.org',
'@type': 'WebSite',
'url': '".$baseUrl."',
'potentialAction': {
'@type': 'SearchAction',
'target': '".$baseUrl."'index.php?page=search&sPattern={search_term_string}',
'query-input': 'required name=search_term_string'
}
}
</script>";
return $snippet;
}
To me, this looks messy. There is almost certainly a better way of solving the problem that you are trying to fix.
5 Comments
use single quotes,
$temp = '<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>';
echo '<pre>';
echo htmlspecialchars($temp);
echo '</pre>';
Comments
Use htmlspecialchars:
$jsCode = '<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>';
$jsToDisplay = htmlspecialchars($jsCode);
Comments
You can Use <xmp> tag.
In HTML
<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>
As you describe you want to save it in php variable so.
In php
$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>';