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
Related
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.
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"
Okay so I have a php script and I need to somehow view the value of one of my variables. The thing is this variable is a very long string of XML that got returned from a server. I know it has an error message in it but I need to actually see what it is saying. If I try and Print or echo the value it only displays part followed by a ... or if I use var_dump it does the same. I've even gone as far as trying to echo a javascript alert with the value but that fails because there are single and double quotes in the xml causing the alert quotes not to be recognized correctly. I just need to see the value of this variable. Any advice? Thanks.
Edit:
Actually said that wrong. Echo and print don't display the value correctly because the tags are in <> brackets so it is recognizing as an html tag.
You can use htmlentities to output the XML string so that you can get a plaintext view of it in a browser.
<?php echo htmlentities( $xml_string); ?>
Alternatively, you can parse the XML string to reveal the error message, but this may be more complicated than what you need.
Try echo htmlentities($var, ENT_COMPAT, 'UTF-8')
i always use this:
echo "<pre>". htmlentities($s) . "</pre>";
Try this:
echo '<pre>'.$xml_string.'</pre>';
See also:
CDATA - (Unparsed) Character Data
i usaly use:
echo nl2br(str_replace('<', '<', $xml));
as its only the < that are a problem
You could just save the XML string to a file. If it's well-formed XML, you can view it with every browser (and expand/collapse nodes ^^).
When I output $_SERVER['REQUEST_URI']; on:
http://localhost/tools/?tool=cs&sub=1
I get:
/tools/?tool=cs⊂=1
Is there other solution to get /tools/?tool=cs&sub=1 besides using & instead of & ?
It's because you're echoing it to your browser - &sub is being interpreted as an HTML entity (⊂).
If you echo htmlentities($_SERVER['REQUEST_URI']); you'll get what you expect.
You have to use the right encoding for the environment you're in - in HTML that means using &.
try this
echo urldecode($_SERVER['REQUEST_URI']);
How are you outputting this value? If you're dumping it to the browser, are you sure it's not trying to 'decode' embedded ampersands?
Try a file with just
<?php phpinfo();
and look to see what the value is displayed as (near the bottom)