i have a problem with some alphanumeric strings containing the exponential char "E", these are stored into the db in a "character varying" column, so are strings, but when i try to visualize them in the web page i get an "INF" string instead of the original.
For example the following "55E77583" (that for me must be only a code number of an order) becomes "INF" in the webpage.
i've tried to search a solution and i found the sprintf and printf commands, but after some tries with differents %char combinations i'm not able to obtain the original form of the string.
$code = "55E77583";
echo sprintf('%s', $code);
//Gives me "INF"
$code = "55E77583";
printf('%s', $code);
//Gives me always "INF"
I really need to obtain the original form of the string, always, in all the possible alphanumeric combinations. How can i do?
Thank you.
If I understand correctly you want to display the value 55E77583 as string on your webpage. Your provided code above will exactly do that.
So somehow your variable must be converted to double or float before, this is why you receive INF because the number is too large to handle with PHP.
Make sure your variable is actually a string by echoing
echo gettype($code);
This will very likely produce "double". Maybe a type conversion is happening during your select.
Related
I’m trying to parse some strings in some messed-up CSV files (about 100,000 rows per file). Some columns have been squished together in some rows, and I’m trying to get them unsquished back into their proper columns. Part of the logic needed there is to find whether a substring in a given colum is numeric or not.
Non-numeric strings can be anything, including strings that happen to begin with a number; numeric strings are generally written the European way, with dots used for thousand separators and commas for decimals, so without going through a bunch of string replacements, is_numeric() won’t do the trick:
\var_dump(is_numeric('3.527,25')); // bool(FALSE)
I thought – naïvely, it transpires – that the right thing to do would be to use NumberFormatter::parse(), but it seems that function doesn’t actually check whether the string given as a whole is parseable as a numeric string at all – instead it just starts at the beginning and when it reaches a character not allowed in a numeric string, cuts off the rest.
Essentially, what I’m looking for is something that will yield this:
$formatter = new \NumberFormatter('de-DE', \NumberFormatter::DECIMAL);
\var_dump($formatter->parse('3.527,25')); // float(3527.25)
\var_dump($formatter->parse('3thisisnotanumber')); // bool(FALSE)
But all I can get is this:
$formatter = new \NumberFormatter('de-DE', \NumberFormatter::DECIMAL);
\var_dump($formatter->parse('3.527,25')); // float(3527.25)
\var_dump($formatter->parse('3thisisnotanumber')); // float(3)
I figured perhaps the problem was that the LENIENT_PARSE attribute was set to true, but setting it to false ($formatter->setAttribute(\NumberFormatter::LENIENT_PARSE, 0)) has no effect; non-numeric strings still get parsed just fine as long as they begin with a number.
Since there are so many rows and each row may have as many as ten columns that need to be validated, I’m looking at upwards of a million validations per file – for that reason, I would prefer avoiding a preg_match()-based solution, since a million regex match calls would be quite expensive.
Is there some way to tell the NumberFormatter class that you would like it to please not be lenient and only treat the string as parseable if the entire string is numeric?
You can strip all the separators and check if whatever remains is a numeric value.
function customIsNumeric(string $value): bool
{
return is_numeric(str_replace(['.', ','], '', $value));
}
Live test available here.
You can use is_numeric() to check that it is only numbers before parsing. But NumberFormatter does not do what you are looking for here.
I have a method that scrapes data from a url and returns that as a string variable. Currently the method is working if i put in my own url, but when i insert a generated url it doesnt work.
e.g.
The following string is working if I insert it into a variable, and pass it:
http://www.rijkswaterstaat.nl/apps/geoservices/rwsnl/awd.php?mode=html&projecttype=windsnelheden_en_windstoten&category=1&loc=ZBWI&net=LMW
But this string is being generated by another source. The result of my attempt to fetch it is (var_dump()):
string(154) "http://www.rijkswaterstaat.nl/apps/geoservices/rwsnl/awd.php?mode=html&projecttype=windsnelheden_en_windstoten&category=1&loc=ZBWI&net=LMW"
The string is only 138 characters, however it prints string(158). I think this has something to do with the fact it is not working, but i'm not even sure...
Does anyone have any idea how to clean this up? I have found other questions with the question why var_dump() is showing another value then the length of the string, and that had something to do with unvisible characters, but no real solution is given anywhere.
Thx
154-138 = 16
You have 4 & in the string
& HTML encoded is &
So your string seems to be HTML encoded - in the browser you don't see the encoding unless you "View Source".
You can use html_entity_decode() to decode the string or, if possible, make sure that you get a string that is not encoded for HTML output in the first place.
I've really been wracking my brains over this one, as for the life of me I can't figure out what the problem is.
I've got some data I want to run a regular expression on. For reference, the original document is encoded in iso-8859-15, if that makes any difference.
Here is a function using the regular expression;
if(preg_match("{£\d+\.\d+}", $handle)) //
{
echo 'Found a match';
}
else
{
echo 'No match found';
}
No matter what I try I can't seem to get it to match. I've tried just searching for the £ symbol. I've gone over my regular expression and there aren't any issues there. I've even pasted the source data directly into a regular expression tester and it finds a complete match for what I'm looking for. I just don't understand why my regular expression isn't working. I've looked at the raw data in my string that I'm searching for and the £ symbol is there as clear as day.
I get the feeling that there's some encoded character there that I just can't see, but no matter how I output the data all I can see is the £ symbol, but for whatever reason it's not being recognised.
Any ideas? Is there an absolute method to viewing raw data in a string? I've tried var_dump and var_export, but I do get the feeling that something isn't quite right, as var_export does display the data in a different language. How can I see what's "really" there in my variable?
I've even saved the content to a txt file. The £ is there. There should be no reason why I shouldn't be able to find it with my regular expression. I just don't get it. If I create a string and paste in the exact bit of test my regular expression should pick up, it finds the match without any problems.
Truly baffling.
You could always transform the letter:
$string = '£100.00';
if(preg_match("/\xa3/",$string)){
echo 'match found';
}else{
echo 'no matches';
}
You can include any character in your regular expression if you know the hexadecimal value. I think the value is 0A3H, so try this:
\xa3 // Updated with the correct hex value
I'm using Google's currency API to fetch exchange rates and store them in my database, but I ran into some problems. Here's what I'm working with:
http://www.google.com/ig/calculator?hl=en&q=100USD=?GBP
I'm always passing 1USD as the first parameter and exchange that to all the currencies in my database, cast the result variable as a float and store it. Everything works fine until the result from the API is greater than 1000. For example:
http://www.google.com/ig/calculator?hl=en&q=100USD=?PYG
This returns "440 528.634" as the value, and the problem is in the space delimiter. When I cast that to a float it only stores "440". I tried running str_replace() on it before i cast it to a float, but for some reason that doesn't work - I'm guessing it's not a regular whitespace but a special character of some sort. I also tried exploding the variable by a space and returning the merged array fields, but no dice. I'm running out of ideas here so I really hope someone can help me on this :D
Its a non-breaking space character. You can replace it if you refer to it as \xA0:
$result = str_replace("\xA0", "", $result);
Note the double quotes. Use those instead of single quotes as it won't work correctly otherwise.
Its NO BREAK SPACE. You should refer to it as \xA0
$x = str_replace("\xA0", "", $x);
Should work.
I am trying to obtain a number from a function which only echos text instead of returning it to a variable as follows:
ob_start();
function_to_get_id_number();
$thisIDnumber = ob_get_clean();
If I echo the $thisIDnumber variable, the desired number is printed in the HTML output.
However, running var_dump($thisIDnumber) outputs the following: string(18) "7"
(Assuming the number was 7, although var_dump() reports an 18-character string regardless of what the number might be.)
Any attempt to convert the string to an integer (e.g. (int)$thisIDnumber , or int_val($thisIDnumber), or $thisIDnumber = 0+$thisIDnumber fails and the output is 0)
Running mb_detect_encoding($thisIDnumber) reports the string to be ASCII encoded.
I'm not really sure how to get around this, but would very greatly appreciate any suggestions or insights! Many thanks in advance!
The string(18) part could be explained if the function prints lots of white space (spaces, tabs or even carriage returns) and you inspect var_dump()'s output through a web browser (so it renders as HTML and spaces are collapsed). However, casting to number should ignore regular leading spaces. So there's possibly some other non-printable character out there.
Try this:
ob_start();
function_to_get_id_number();
$thisIDnumber = ob_get_clean();
var_dump($thisIDnumber, bin2hex($thisIDnumber));
The hexadecimal code should give you a clue of what's inside the string.
Update:
$data = '5b777073635f63617465676f72795f69645d';
for($i=0, $len=strlen($data); $i<$len; $i+=2){
echo chr(hexdec(substr($data, $i, 2)));
}
... prints this:
[wpsc_category_id]
:-?
You have not provided much information to work with, so this is a shot in the dark.
Why don't you try this:
If the id is numeric by definition, enforce it by removing everything else using a regular expression.
(As jproffit wrote in his comment to your question: more info on function_to_get_id_number() would be nice. Why do you need to buffer the output in the first place? Can't function_to_... return a proper value instead of outputting a string?)