Hi I am performing an SQL query (that only returns one row) and I am printing a particular column on screen just for test purposes. The problem I am having is, the column that I am printing contains a string of 12 characters (abcdef123456), yet sometimes the printed variable is empty.
So when I do var_dump($test);
sometimes I get:
string(12) "abcdef123456"
but some other times I get
string(12) ""
I dont understand why it knows there are 12 characters yet its still empty, and sometimes it says 12 characters yet its full. Due to this, I cant perform other functions as they rely on the string.
EDIT: here is the query
$query="SELECT * FROM members WHERE name='$member'";
$sqlResult=mysql_query($query);
$row = mysql_fetch_assoc($sqlResult);
$test = $row["membercode"];
var_dump($test);
You most-likely have html tags in your string that are being rendered by the browser.
$foo = "<p><img><div><table>";
var_dump($foo); // string(20) ""
Where do you see that it says string(12) ""? Remember to ALWAYS look in the raw output; the source code. Not how the browser renders it.
For instance, if the string is <span></span>, that'll show up as nothing.
Try adding header('Content-Type: text/plain') to render your page as text instead of HTML. Or use "View Source" to view the HTML source instead of the rendered page. There may be hidden HTML tags in the string.
Try var_dump(bin2hex($test)). There may be invisible control characters such as "\0" that you can't see. For example:
$ php <<< '<?php var_dump("\0\0\0\0"); ?>'
string(4) ""
$ php <<< '<?php var_dump(bin2hex("\0\0\0\0")); ?>'
string(8) "00000000"
Related
Using the following in PHP:
var_dump($obj->denormalized->{'https://api.site.com/user/user-76643221'}->data->avatar_image)
Results:
string(56) "https://api.site.com/image_link/user-76643221_b691647a"
I only need the link, without those extra parts. Any ideas?
var_dump shows the types and sizes of variables; it's meant for debugging. If you only need the link, then instead of using var_dump use print or echo
echo $obj->denormalized->{'https://api.site.com/user/user-76643221'}->data->avatar_image;
Results:
https://api.site.com/image_link/user-76643221_b691647a
I am trying to receive the content of the <tbody> tag from this page.
There are only one table with only one tag <tbody>, and i want to get all rows from this table
I try to do this by this way
$page = file_get_contents('http://pk.zntu.edu.ua/fakultety-ta-napryamy-pidhotovky/derzhavne-zamovlennya-2011-bakalavr');
preg_match_all("/<tbody>(.+?)<\/tbody>/is", $page, $output_array);
var_dump($output_array);
And i receive empty arrays:
array(2) { [0]=> array(0) { } [1]=> array(0) { } }
I have tried different variants of patterns like
"/<tbody>(.*?)<\/tbody>/is"
"/<tbody>.+?<\/tbody>/is"
"/<tbody>.*?<\/tbody>/is"
"/<tbody>.+<\/tbody>/is"
"/<tbody>.*<\/tbody>/is"
But no one works
With PCRE and Regex Library all should be okay
I don't know what's the problem, please help
Your pattern it's very simple, the regex above should be fine. but I think the problem is come from file_get_contents. I just try to count number of lines in $page variable and i get this
71220
But the real code that I check by clicking into that website and copy source code then count it manually, it's about 1787 lines.
What does this mean?
It maybe means that the code that you store it in $page variable is not the same as HTML code that you see when you manually click into that website. In actually when you open one website, many thing can be occurred e.g listener method is working, but in case that you download those source code directly to PHP variable some methods maybe never executed and this can make you get an incomplete HTML code.
Note that the another evidence that support my assumption is I can not even find a keyword tbody in your $page variable.
tbody tag may also contain attributes. So you need to match that attributes also in-order to get the content of tbody tag.
'/<tbody\b[^>]*>(.*?)<\/tbody>/is'
I have a PHP associative array that I am trying to extract data from:
array(18) { ["body"]=> string(34) "Hey! Let me know if you got this"}
The above array is stored in a variable called $firstChildData, and when I try to run the following line, I get the result below it:
$firstChildBody = $firstChildData["body"];
This returns: string(34) "Hey! Let me know if you got this"
Does anyone know how to remove the 'string(34)' so I can just have the value within the quotes? I have tried to use the explode() function with " as the delimiter, but that didn't work.
Thanks in advance!
string(34) is just a debug information if you use a function like var_dump or print_r.
You can print strings normal with echo or print.
echo $firstChildData["body"];
If you want to use the variable $firstChildData["body"] in any program code, it is handled as the string in it. The information string(34) ist not included.
Thanks everyone!
You're right, I was using var_dump, which was showing that information even thought it was not part of the string. I used echo instead and it works beautifully.
I am fetching '$' symbol from uri.
And I have already added $ in permitted_uri_chars in config file.
Later I am fetching data from database using that uri string.
issue can be seen from below example.
MY Url is like this ....
.....com/search/shirt/$
My Example code is this ...
echo $this->uri->segment('3');
echo "<br>";
var_dump($this->uri->segment('3'));
echo "<br>";
$dol_sign = '$';
echo $dol_sign;
echo "<br>";
var_dump($dol_sign);
MY Output of above code is this ...
$
string(5) "$"
$
string(1) "$"
it is taking segment as differently. that is why i m having issue in fetching data from DB matching with URI segment.
Your first sign is an HTML encoded entity - if you see the source of the page you'll see & instead of a simple &.
You can use the PHP html_entity_decode() function to decode it into the normal character, or test against it.
A string I'm working with in PHP comes from a text file and the line reads:
<Unknown>
However when I var_dump the array variable associated to this line, it reads
string(9) ""
I've inserted the same data <Unknown> before into MySQL and have never had this problem.
That's because you are var_dumping it and using your web browser to see the output, anything between < and > is regarded as a HTML tag by your browser, thus you see no output but with string(9)
You can try:
echo '<pre>' . print_r($var, 1) . '</pre>';
If you would like to surely see:)))
$var = "<unknown>";
var_dump(htmlspecialchars($var));
http://php.net/manual/en/function.htmlspecialchars.php