首页 > 解决方案 > php从网页解析字符串

问题描述

是否有替代代码可以忽略 <td *******>1234</td> 我只需要 1234 并且可以忽略 td 字符串。

// 现在我正在剥离字符串,但在现场情况下,这个字符串也可以更改,所以我被卡住了。

$searchString = '<td style="font-weight: bold; font-size:15px; text-align:left;">Pakistan</td>';
    $i=1;
    foreach($stringArr as $key) {  

    if( strpos( $key, $searchString ) !== false )
        $LineNum = $i;

$i++;
}   
$LineNum = $LineNum-1;


$Country = trim($stringArr[$LineNum]); //get the third line
$TotalCases = trim($stringArr[$LineNum+1]); //get the third line
$NewCases = trim($stringArr[$LineNum+2]); //get the third line
$TotalDeaths = trim($stringArr[$LineNum+3]); //get the third line
$NewDeaths = trim($stringArr[$LineNum+4]); //get the third line
$TotalRecovered = trim($stringArr[$LineNum+5]); //get the third line
$ActiveCases = trim($stringArr[$LineNum+6]); //get the third line
$Serious_Critical = trim($stringArr[$LineNum+7]); //get the third line
$Tot_Cases_1M_pop = trim($stringArr[$LineNum+8]); //get the third line
$Deaths_1M_pop = trim($stringArr[$LineNum+9]); //get the third line
$Reported_1st_case = trim($stringArr[$LineNum+10]); //get the third line
 $Country = substr($Country, 64); // for now i am stripping the string but on live case this string can be changed as well so i am stuck.
 $Country = substr($Country,0,-5); 
  $TotalCases = substr($TotalCases, 48); 
 $TotalCases = substr($TotalCases,0,-5); 
 if($NewCases == "<td style=\"font-weight: bold; text-align:right;\"></td>")
{
    $NewCases = 0;
}else{
     $NewCases = substr($NewCases, 74); 
}

标签: phpsearch

解决方案


strip_tags 将删除 html 标签


推荐阅读