Unable to str_replace space
I try to pull out the number string from google and clean it up.
<?php
$q="35 meter in inch";
$query = explode (" ",$q);
$googleUrl="http://www.google.com/search?q=$query[0]+$query[1]+$query[2]+$query[3]";
$package = file_get_contents("$googleUrl");
$content = preg_replace('/.*<h2[^>]* style="font-size:138%"><b>|<开发者_开发技巧;\/b><\/h2>.*/si', "", $package) ;
$number = explode (" ",$content);
$result = str_replace(' ','',$number[3]);
echo $result;
?>
however, the number i've got has a space. I tried to replace it with needles " " or "  ;". Or utf8_encode, decode $content. None of them works.
As for the solution to your problem, the best answer is to replace anything that is not a number or punctuation using preg_replace(); Try this:
<?php
$q="35 meter in inch";
$query = explode (" ",$q);
$googleUrl="http://www.google.com/search?q=$query[0]+$query[1]+$query[2]+$query[3]";
$package = file_get_contents("$googleUrl");
$content = preg_replace('/.*<h2[^>]* style="font-size:138%"><b>|<\/b><\/h2>.*/si', "", $package) ;
$number = explode (" ",$content);
$result = preg_replace("/[^\d.]/", '', $number[3]);
echo $result;
?>
But you may want to look into using google.com/ig/calculator. It should save a lot on bandwidth and save you having to pull a full Google Results page and replace on it: http://www.google.com/ig/calculator?hl=en&q=35%20meter%20in%20inch
<?php
$q="35 meter in inch";
$query = explode (" ",$q);
$googleUrl="http://www.google.com/ig/calculator?q=$query[0]+$query[1]+$query[2]+$query[3]";
$content = file_get_contents("$googleUrl");
preg_match("/rhs:\s\"(.*)\",error/", $content, $number);
$num = explode(" ", $number[1]);
$num = preg_replace("/[^\d.]/", '', $num[0]);
echo $num;
?>
Probably because it's not really a space, even though it looks like it. You could try replacing all \w
with the regular expression.
hi the space before <?php
tag it it there in your code too? then that might be giving the space check that!
This is not a space you are trying to remove, it is "à" that is not visible in browser. You can also check these things by using your php script by commandline. You can use html entities function and then replace according to that
精彩评论