This question already has answers here:
Is there a better way to write HTML strings in PHP?
(8 answers)
Closed 8 years ago.
My PHP code begins with the ?php tag and ends with the ? tag and my file extension is php. If I want to write part of my PHP code inside HTML, what is the general way of doing it?
For instance:
echo '<strong>' .$element .'</strong><br>';
Do I need to always use single quotes for the HTML tags used inside of a PHP file? Or double quotes or no quotes?
The first thing you need to learn is that PHP doesn't care about HTML.
PHP doesn't understand that it's working with HTML, it just sees text and dutifully sends it to the browser. The browser then makes sense of it as HTML.
So, since <strong> is a string, it must be treated as a string. In other words, as "<strong>", '<strong>' or even a heredoc:
<<<HEREDOC
<strong>
HEREDOC;
(Probably not appropriate in this case :p)
Related
This question already has answers here:
Mixing a PHP variable with a string literal
(5 answers)
Closed 4 months ago.
My actual use case involves filling in javascript variable names with partial id's from php, but to illustrate the issue here's a simple example with html:
$var="ello worl";
echo <<<HTML
H$var d
HTML;
I want the output to be "Hello world" however of course there is a space after the $var variable name so the output is "Hello worl d". If I remove the space, then it changes the variable name.
How do I place text next to the right side of the variable?
I've tried quotes and escaping etc. but to no avail.
You can enclose the variable in curly braces ({ and }) to ensure the PHP interpeter knows which characters are part of the variable name and which are static text.
$var="ello worl";
echo <<<HTML
H{$var}d
HTML;
Demo: https://3v4l.org/TYgEF
Documentation reference: https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I am hitting a road block with a script I have to check availability on a certain website. I need the text within html tags and I am unsure how to approach it.
My code I have tested ended with this:
<?php
ini_set("allow_url_fopen", 1);
$homepage2 = file_get_contents('https://www.someurlwithavailability.com');
//URL has the following HTML tag: <div id="Availability">
Availability: Special Offer, ships within 10 - 15 business days </div>"
preg_match("/<div id="Availability">(.*?)</div>/si", $homepage2, $avail);
print_r($avail);
echo '<br>', '~Availability is~', '<br>', $avail, '<br>';
$stringavail=implode(" ",$avail);
echo $stringavail;
?>
I get various errors depending on what I put after preg_match(***,$homepage2, $avail); and I am unsure about what syntax I need to enter to retrieve the text.
My code above gives me this:
Parse error: syntax error, unexpected 'Availability' (T_STRING) in /u/o/placeiamrunningthecodefrom.php on line 6
The URL that is requested comes back with a full HTML page that is quite large. This HTML tag is unique and does not repeat.
Anyone able to help me out?
Although this can work just fine with regex. It's not recommended, nor easier.
Id suggest giving DOMDocument::getElementById a go. It even has an example right on the page:
$doc = new DomDocument;
// We need to validate our document before refering to the id
$doc->validateOnParse = true;
$doc->Load('book.xml');
echo "The element whose id is 'php-basics' is: " . $doc->getElementById('php-basics')->tagName . "\n";
Now to get the content instead of tagName we can use ->textContent as inherited from domnode
Try using single quotes around that pattern.
And, make sure you are escaping the special regex characters.
And, you are essentially asking for everything to the last </div>. So, you need to be more specific.
'/<div id="Availability">([^<]*)<\/div>/si'
instead of
"/<div id="Availability">(.*?)</div>/si"
Of course, this could still be unreliable if there is html in that the <div>
But, this should get you closer.
Also, try an online regex tool. I like this one.
https://regex101.com/
The problem is that you have double quotes inside your double-quoted string, and didn't escape them:
preg_match("/<div id="Availability">(.*?)</div>/si", $homepage2, $avail);
^ ^
If you used a decent IDE it would have alerted you to this as you were typing.
Simply change the delimiting quotes to single quotes.
Also, since your regexp delimiter / appears in the regular expression, you either need to escape the character where it appears in the regexp, or use a delimiter that isn't in the expression.
preg_match('#<div id="Availability">(.*?)</div>#si', $homepage2, $avail);
However, using regular expressions to parse HTML is generally a bad idea. You should use a DOM parser library like the DOMDocument class.
This question already has answers here:
PHP quotes inside quotes
(3 answers)
Closed 5 years ago.
hi guys i just want to know if there is an alternative syntax for onclick="document.getElementById('id01').style.display='none'" on php. Beceause its not working even it is inside the echo.tnx
here is my code
First up: document.getElementById is a construct used in JavaScript and has nothing to do with PHP.
The problem you are running into is that you are not escaping the single quotes in your string:
echo '<button onclick="document.getElementById(' <- there is your problem
PHP thinks that the string ends here since you told it so with '.
But the string is in fact not at its end so you need to escape the single quote in order to make it work:
echo '<button onclick="document.getElementById(\'id0\')...';
This goes for all single quotes in your string of course.
This question already has answers here:
PHP using Gettext inside <<<EOF string
(2 answers)
Closed 8 years ago.
Is it possible to put php code ( NOT ONLY {$SomeVariable} ) inside eof ?
bad syntax but just to get the idea...
$html = <<<EOF
<!docutype html>
<html>
<head>
<title>{ SOME PHP CODE }</title>
</head>
EOF;
No, heredoc strings are the same as double-quoted strings with the exception that the double-quote character does not need to be escaped. Variables are expanded in the same way (including complex variables as well as simple variables as in your example). You cannot run code other than this within the string.
I'd recommend reviewing the strings documentation:
http://www.php.net/manual/en/language.types.string.php
Also your example is invalid, the closing operator (EOF; in this example) cannot be indented.
This question already has answers here:
How to remove text between tags in php?
(6 answers)
Strip HTML tags and its contents
(2 answers)
Closed 9 years ago.
I want to remove part of string between two html tags. I have something like this:
$variable = "This is something that I don't want to delete<blockquote>This is I want to delete </blockquote>";
the problem is that the string between blockquote tag is changing, and its need to be deleted, no matter what it is. Anyone now how?
Regex are not the best thing to parse html string, you should take a look at Simple HTML dom parser or the php DOMDocument class.
If you still want to use a regex in this case it will be for example :
$variable = preg_replace('/<blockquote>.+<\/blockquote>/siU', '', $variable);
Test it there.
You can use regular expressions, but this is by no means fail-safe and should only be used in trivial cases. A better way is to use a full-fledged HTML parser.
<?php
$str = preg_replace('#<blockquote>.*</blockquote>#siU', '', $str);
?>
please try using regex in this way
<?php
$variable = "This is something that I don't want to delete<blockquote>This is I want to delete </blockquote>";
$str = preg_replace('#(<blockquote>).*?(</blockquote>)#', '$1$2', $variable);
print($str);
?>