Attualmente l'unico command PHP globale che conosco è:
<?=$text_items?>
Questo sputa:
1 item(s) - £318.75
Voglio get il valore 318.75
quindi al momento sto provando una sostituzione ma non funziona tutto liscio:
$short = $text_items; $short = str_replace("£", "", $short); $short = str_replace("£", "", $short); $short = str_replace("-", "", $short); $short = str_replace("–", "", $short); $short = str_replace(" ", "", $short); $short = str_replace("-", "", $short); $short = str_replace("ITEMS", "", $short); $short = str_replace("(", "", $short); $short = str_replace(")", "", $short); $short = str_replace("item(s)", "", $short); $short = str_replace("ITEM", "", $short);
$total = @floatval(end(explode('£', html_entity_decode($text_items))));
html_entity_decode
changes £
a £
end(explode('£'
ti dà una string dopo il carattere ' £
' floatval
sta valutando la string su float. E_STRICT
che si verifica passando alla costante nella function end()
. Esempio di lavoro
La seconda soluzione è Regexp:
preg_match_all('!\d+(?:\.\d+)?!', $text_items, $result); echo $result[1];
Esempio di lavoro