php - String get from CDATA, returned 0 when was converted to "int" -
i text include in cdata looks in xml file:
<?xml version="1.0" encoding="utf-8"?> <kml xmlns="http://splashurl.com/nztqga8 <document> <name>opencellid cells</name> <description>list of available cells</description> <placemark><name></name><description><![cdata[lat: <b>3.378199</b><br/>lon: <b>-76.523528</b><br/>mcc: <b>732</b><br/>mnc: <b>123</b><br/>lac: <b>4003</b><br/>cellid: <b>26249364</b><br/>averagesignalstrength: <b>0</b><br/>samples: <b>10</b><br/>changeable: <b>1</b>]]></description><point><coordinates>-76.523528,3.378199,0</coordinates></point></placemark> </document> </kml>
i cdata text usign next code in php :
$ch = curl_init(); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_url, $url); // url contents $data = curl_exec($ch); // execute curl request curl_close($ch); $xml = simplexml_load_string($data,'simplexmlelement', libxml_nocdata | libxml_noblanks); // permite foreach ($xml->document->placemark $mensaje) $resuktado[]= (string) $mensaje->description.' ';
how need lat, lon , cellid values, using this:
$string_array = explode("<br/>",$resuktado[$i]);// separar por salto de linea el string $latitud= explode(" ",$string_array[0]);
and lattitud[1] lat value.
whe show value in scream looks this: 3.378199
but whe convert int value using:
echo (int) lattitud[1]; returns 0.
how can convert string int valule represents
the problem in code $latitud[1] contains string <b>3.378199</b>
, not number self. there various ways fix it. here one, using regular expression;
preg_match("/\<b\>([^\<]+)/", $latitud[1], $match); $lat=$match[1]; // outputs 3.378199
by way, in php, not need convert between string , int.
$a="1"; echo $a+1;
puts 2. if feel need convert it, try
intval().
if want rid of decimal use 1 of theese:
floor(); ceil(); round();
Comments
Post a Comment