[英]Extract the number from a string in PHP
如何從PHP的以下字符串中提取數字12345?
<span id="jordan934" itemprop="distance"><span class='WebDistance'>#$@20B9; </span>12345</span></h3>
我一直在使用以下命令,直到其中沒有'#$ @ 20B9'字符串為止。
$results = $dom->query('#jordan934"]');
$distance = false;
if (count($results)) {
$distance = (int)trim($results->current()->textContent);
}
return $distance;
}
嘗試使用正則表達式
$str = 'jordan934';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
您可以使用dom對象
<?php
$html = '<span id="jordan934" itemprop="distance"><span class=\'WebDistance\'>#$@20B9; </span>12345</span></h3>';
$dom = new DomDocument();
$dom->loadHTML($filePath);
$finder = new DomXPath($dom);
$classname="my-class";
$nodes = $finder->query("//*[contains(@class, '$classname')]");
var_dump($nodes);
其他尚未廣為人知的功能是:
$str = 'jordan934';
$int = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.