substr not returning a value - php

I'm trying to execute the following simple command, but it's not returning any result:
$val = substr($gesprek->gevormdnummer, 0, 2);
The $gesprek->gevormdnummer value is a phone number fetched from the database (stored as varchar(50)).
I'm using the Yii framework to get the data from the MS SQL Database.
I'm running PHP 5.5.8 NTS on IIS7.5.
If I echo the variable it returns for example 00497121212 or 511 depending if it's an internal or external number.
When I change $gesprek->gevormdnummer to for example '511', substr will work correctly. I've tried using mb_substr with UTF-8 encoding but that returns the same result.
Is there anybody who has an idea what the problem might be?

CBroe gave the to finding the answer to my problem.
The string was prefixed with spaces till there we're 16 characters.
So the substr was working correctly, i got back 2 spaces, which didn't show up in the echo.
var_dump($var) gave me the insight to find this out.

Related

DOMDocument and UTF8. MySQL says: Incorrect string value

I am trying to load the meta description of this website (which has a German character) via the following script in PHP:
$page_content = file_get_contents($uri);
$dom_obj = new \DOMDocument();
$dom_obj->loadHTML(mb_convert_encoding($page_content, 'HTML-ENTITIES', 'UTF-8'));
However, while trying to write it into the MySQL db, Laravel says it ran into troubles trying to write that into the db: incorrect string value "\xC3" (which is the German character)
When I simply do the following, writing to the db works. But the character is not displayed correctly (ü instead of ü)
$dom_obj->loadHTML($page_content)
This problem only occurs with this website so far, others I tried with the same character do work. Can you think of a possible reason and fix? Thank you!
Edit:
It works fine, when I use PHPs "utf8_decode" to decode the meta description that I get via $dom_obj without mb_convert_encoding. When I do this, all other sites that worked before lead to errors (like this: Incorrect string value: '\xE4t')
I found the error. I was using substr to shorten the description. Apparently substr cut off one of those special characters and this is why it wasnt working.
foreach($dom_obj->getElementsByTagName('meta') as $meta) {
if($meta->getAttribute('name')=='description'){
substr($meta->getAttribute('content'), 0, 156);
This is a workaround:
mb_substr($foo,0,156,"UTF-8");

PHP & OCI query returns NUMBER columns as STRING

I'm using PHP5 and OCI 8 with Oracle 11g.
When I fetch a row using oci_fetch_all, the whole result is converted as STRING even for the NUMBER columns and even if I use Oracle's TO_NUMBER in the query.
What I'm trying to do is simple: the javascript calls the PHP script through an Ajax request. The script just fetch some NUMBER data and encode them into JSON. I want the data to be encoded as integer, so the javascript can do math stuff on it (add, divide,..etc) without any conversion.
I am pretty sure that the problem comes from OCI and not JSON encoding because when I VAR_DUMP the result of oci_fetch_all, I can clearly see double quotes on every result:
{
"COLUMN1":"12",
"COLUMN2":"52"
}
I want the result to look like this:
{
"COLUMN1":12,
"COLUMN2":52
}
I tried to:
Change the flag of oci_fetch_all (OCI_FETCHSTATEMENT_BY_ROW, OCI_FETCHSTATEMENT_BY_COLUMN...)
Use oci_fetch_array instead of oci_fetch_all
Remove the UTF8 encoding on the connexion to oracle (I know, its stupid)
The strange thing is that I can't find any thing on the internet about this problem... It's like nobody faced the same issue. Maybe i'm doing something wrong...
Thanks in advance
You can use an extra option in json_encode:
json_encode($rows, JSON_NUMERIC_CHECK);
However this option requires a PHP version of 5.3.3 or higher (thus its ok for you).
All database extensions in PHP work like this, there's nothing you can do about it.
You'll have to manually type-cast the database results.

php, string value, length and integer value of a variable don't matche

I am getting a variable from a socket. After reading the respond I am trying to convert the value to a number. if I print the respond it looks correct however when I convert it to number using floor() it doesn't give me the right answer. I also tried to print the length of the variable and it is still not working as it suppose to: This one is for value :185
echo("**************** ".floor($res[0]));
echo "################### $res[0]";
echo "------------- ".strlen($res[0]);
output:
**************** 1################### 185------------- 12
I have also tried stripslashes, trim and also ereg_replace('[:cntrl:]', '',$res[0])
Please try the following: intval($res[0])
You can try also with:
$res = (int)$reg[0];
Hope this helps.
Bye
Ok I found the problem. I saved the value in a file and opened the file using notepad++. I saw the following character in between my string:
SOH,NULL, and bunch of other non character values
What I am assuming is PHP automatically ignore the ASCII values are not show able on the screen (less than 21Hex).

PHP json_decode returns null

I'm writing PHP code that uses a database. To do so, I use an array as a hash-map.
Every time content is added or removed from my DB, I save it to file.
I'm forced by my DB structure to use this method and can't use mysql or any other standard DB (School project, so structure stays as is).
I built two functions:
function saveDB($db){
$json_db = json_encode($db);
file_put_contents("wordsDB.json", $json_db);
} // saveDB
function loadDB(){
$json_db = file_get_contents("wordsDB.json");
return json_decode($json_db, true);
} // loadDB
When echo-ing the string I get after the encoding or after loading from file, I get a valid json (Tested it on a json viewer) Whenever I try to decode the string using json_decode(), I get null (Tested it with var_dump()).
The json string itself is very long (~200,000 characters, and that's just for testing).
I tried the following:
Replacing single/double-quotes with double/single-quotes (Without any backslashes, with one backslash and three backslashes. And any combination I could think of with a different number of backslashes in the original and replaced string), both manually and using str_replace().
Adding quotes before and after the json string.
Changing the page's encoding.
Decoding without saving to file (Right after encoding).
Checked for slashes and backslashes. None to be found.
Tried addslashes().
Tried using various "Escape String" variants.
json_last_error() doesn't work. I get no error number (Get null, not 0).
It's not my server, so I'm not sure what PHP version is used, and I can't upgrade/downgrade/install anything.
I believe the size has something to do with it, because small strings seem to work fine.
Thanks Everybody :)
In your JSON file change null to "null" and it will solve the problem.
Check if your file is UTF8 encoded. json_decode works with UTF8 encoded data only.
EDIT:
After I saw uploaded JSON data, I did some digging and found that there are 'null' key. Search for:
"exceeding":{"S01E01.html":{"2217":1}},null:{"S01E01.html":
Change that null to be valid property name and json_decode will do the job.
I had a similar problem last week. my json was valid according to jsonlint.com.
My json string contained a # and a & and those two made json_decode fail and return null.
by using var_dump(json_decode($myvar)) which stops right where it fails I managed to figure out where the problem was coming from.
I suggest var_dumping and using find dunction to look for these king of characters.
Just on the off chance.. and more for anyone hitting this thread rather than the OP's issue...I missed the following, someone had htmlentities($json) way above me in the call stack. Just ensure you haven't been bitten by the same and check the html source.
Kickself #124

Problem reading data from file special characters

My previous question and this question both are related a bit. please have a look at my previous question I did not found any other way to unserialize the data so coming with the string operation
I am able to get the whole content from file but not able to get the specific string from this content.
I want to search a specific string from these content but function stop working when the reach at first special character in the string. If I am searching something found before the special character the works properly.
String operation function of PHP not working properly when the encounter first special character in the string and stop processing immediately, Hence they does not give me the correct output.
Originally they looks like (^#)
:"Mage_Core_Model_Message_Collection":2:{s:12:"^#*^#_messages";a:0:{}s:20:"^#*^#_lastAddedMessage";N;}
but when I did echo they are display as ?
Here is the code what I tried
$file='/var/www/html/products/var/session/sess_ciktos8icvk11grtpkj3u610o3';
$contents=file_get_contents($file);
$contents=htmlspecialchars($contents);
//$contents=htmlentities($contents);
echo $contents;
$restData=strstr($contents,'"id";s:4:"');
echo $restData;
$id=substr($restData,0,strpos($restData,'"'));
echo $id;
I changed the default_charset to iso-8859-1 and also utf-8 but not working with both
Please let me know How I can resolve this.
Thanks.
These characters that you see as ^# are actually null bytes. They don't have any proper display, neither they are meant to be displayed - it's an internal representation of protected properties in the engine. You're not supposed to mess with them.
As for resolving, it'd be nice to know what kind of resolution you seek - what result are you trying to achieve?

Categories