Using a variable in $html = file_get_html(); - php

i can't understand why variables are not working here. Here is the code:
include_once('../simple_html_dom.php');
$url = htmlentities($_GET['q']);
$urlall = str_replace(" ", "+", $url);
$html = file_get_html('http://www.example.com/some?key=$urlall&hl=en');
echo $html->plaintext;
if you look at this code you will found $urlall variable which i have applied in the web address, but this variable can't extract its data. As a new PHP programmer can't understand what to do now to make it works.. Here i have used HTML DOM PARSER..Thanks

Strings inside single quotes are literal, so $urlall is just a string, it won't be replaced with accual variable value. What you want to do is to use double quotes:
$html = file_get_html("http://www.example.com/some?key=$urlall&hl=en");
For futher explanation refer to PHP Strings:
Note: Unlike the double-quoted and heredoc syntaxes, variables and
escape sequences for special characters will not be expanded when they
occur in single quoted strings.

PHP Variables are not parsed and replaced in single quotes.
Try
$html = file_get_html("http://www.example.com/some?key=$urlall&hl=en");

Try to replace single quotes with double:
"http://www.example.com/some?key=$urlall&hl=en"
or use string concatenation instead of direct variable input into string:
'http://www.example.com/some?key='.$urlall.'&hl=en'
Second variant is more preferable.
I hope this helps you

Related

PHP close tag concatenate confusion

eval("\$data = $myvar('https://www.example.com/json/id='". $_GET['name'] ."'))';
This got me an error, how to concatenate it properly?
Eval is dangerous, but for your specific question above, the quotes were off at the end, and myvar is supposed to be a function. See below:
eval('$data = myvar("https://www.example.com/json/id='. $_GET['name'] .'");');
If you use double quotes like "$data" then $data will first be evaluated and the result or value will be eval'd instead. This is one of the risks of using eval(). If you use double quotes, then escape the $ signs like so:
eval("\$data = myvar('https://www.example.com/json/id=". $_GET['name'] ."');");
Demo: IDEOne
eval("\$data = $myvar('https://www.example.com/json/id=". $_GET['name']."');");

eval() function in php not working

i have a little problem with eval.
here is my code:
$postbit = $template->output('posts_list_index'); // output string: $postslist it's ok
eval('echo $postbit;'); // output string: $postslist (?)
Thanks :)
I think in eval() function you can able to replace string with values. Please refer http://in3.php.net/eval.
and the solution for your's is:
eval("$postbit = ".$template->output('posts_list_index'."); ");
echo $postbit ;
try this one.. I am not sure about it.
Javadewd's Answer is correct. I prefer using double quotes instead of single quotes. PHP Manual states
Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
Eval requires that you escape your $:
eval('echo \$postbit;');

How to Return a Stored Value in Inner text using php

PHP Code:
$name = 'click here';
echo '$name';
Here I am Expecting 'Click here' but my Output is:
$name
The PHP Manual addresses this accurately:
Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
To solve the issue, use either one of these solutions:
echo "$name";
echo ''.$name.'';
<?php echo $name; ?>
Either use double quotes around your entire echo statement and escape the quotes in your HTML, or use the concantination operator .
Using double quotes:
"$name";
Using the concantination operator
''.$name.'';
Using double quotes causes PHP to evaluate all variables (replace them with their contents) within the string. However to do this you also have to escape the inner double quotes by making them \" so that PHP doesn't confuse them with the end of the string.
Using the concantination operator you are actually creating 3 different strings, the open tag, the contents of the variable, and the closing tag and then gluing them together using the . to make one complete string which is sent to echo.
Manual reference on strings
http://php.net/manual/en/language.types.string.php
The basics are anything within " (double quotes) is evaluated, anything within ' (single quotes) is not.
So for your code there are a few options
Replace single quotes with double quotes and escape embedded double quotes with \
echo "$name";
You can also replace the embedded double quotes with single quotes (i don't think html minds, not sure about html5)
echo "<a href='http://example.net/some.php' class='menu'>$name</a>";
You could also do a printf, this replace %s = string, with your value $name
printf('%s", $name);

using html and java script tags in php

I am confused about using single and double quotes and back slash while using java script and html tags in php can any one please clarify i googled it but still not clear about it. i am confused for this small thing.i am new to programming
- here is my code
<?php
if(isset($_GET['id'])) {
echo '<div id="d2">';
include "test2.php";
echo '</div>'; }
else
{
echo '<div id="d1">';
include "insert.php";
print "<script type=javascript>"
print "document.getEelementById('alertdiv1').innerHTML='hi' ;"
print "</script>"
echo '</div>';
}
?>
In PHP, you can enclose a string in either single quotes or double quotes. Both are valid:
$var = "this is a string";
$var2 = 'this is also a string';
The main difference is that if your string contains a variable, and you want the variable content to be treated as part of the string, you need to use double quotes:
echo "$var which I made";
will return:
this is a string which I made
When you are manipulating html, css and JavaScript strings, you need to make sure that you don't accidentally close your PHP string. For example:
echo "<h1 class='myheading'>Heading Text</h1>";
Notice how I used double quotes to enclose my string? Because I did that, I was able to use single quotes in the html, without escaping them.
If I'd wanted to use double quotes in my string, I would have had to escape them, like this:
echo "<h1 class=\"myheading\">Heading Text</h1>";
The \ tells PHP that the double quote which immediately follows is to be treated as a literal, and not used to terminate the string.
I can't see any problems relating to quotes in your code.
<script type=javascript> — That is not a valid value of the type attribute (which is optional anyway now). Get rid of the type attribute.
document.getEelementById — Element only has 3 es in it, not 4.
alertdiv1 — There is no element with that id in your code
hi as far as concerned single quotes and double quotes doesnt matters when its a string.but when you use any variable inside
$a = 'hi';
echo '$a' ;
will output
$a
but when you use " "
$a = 'hi';
echo "$a" ;
it will print
hi
Basically, if you're using "" (quotation marks) as your delimiter and you then use a quotation mark as part of the string you must escape it by putting a backslash in front of it.
For example:
$string = "This is my string"; // This is fine as the value of the string doesn't contain any quotation marks (")
$string = "I am including a quote in my string \"To Be Or Not To Be\"."; // This is also fine, as I have escaped the quotation marks inside the string
The same applies if you're using '' (apostrophes) as your delimiter and you then want to use them as part of the string, you must escape them using back slash ().
Hope that helps.
$var = "AAA";
echo 'This costs a lot of $var.'; // This costs a lot of $s.
echo "This costs a lot of $var."; // This costs a lot of AAA.

Why isn't my variable expanded in this string?

$q = $_GET['q'];
// Load and parse the XML document
$rss = simplexml_load_file('http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1');
This returns results containing "$q" instead of results containing the value of $q. Why won't it work?
Change your quotes to double quotes in the simplexml_load_file line
$q = $_GET['q'];
// Load and parse the XML document
$rss = simplexml_load_file("http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1");
PHP will automatically resolve the variable only if the string is in double quotes.
You need to use double quotes to have variables getting expanded:
Variable parsing
When a string is specified in double quotes or with heredoc, variables are parsed within it.
So:
"http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1"
But it would be even better if you use proper URL escaping with urlencode:
'http://search.twitter.com/search.atom?lang=en&q='.urlencode($q).'&rpp=100&page=1'
You can also do that when declaring the $q variable and then use the double quoted string delcaration:
$q = urlencode($_GET['q']);
$rss = simplexml_load_file("http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1");
Use double quotes instead of single quotes.

Categories