我有一个变量$ html
存储此代码的
<form action="track_mobile.asp" method="post" name="TrackMobile">
<table width="99%" height="55" border="0" cellpadding="2" cellspacing="0">
<tr>
<td class="Heading2" colspan="2"> Track Any Mobile Location</td>
</tr>
<tr>
<td width="21" valign="top" rowspan="2" class="s2stextbox" valign="top"><img src="../images/operators_logo/Tata.png" width="134" height="121" align="left"> </td>
<td width="897" valign="top" class="s2stextbox"><font size="2"><b>Mobile Number: </b>918888888888</font></td>
</tr>
<tr>
<td width="897" valign="top" class="s2stextbox" valign="top"><font size="2"> <b>User Name:</b> We are unable to trace the Name for this Mobile Number<br>
<font size="2"><b>Mobile Operator Name:</b> TATA TELESERVICES<br>
<b>State/Region: </b>Maharashtra</font></td>
</tr> </table>
</form>
其中“:( semicollon)”之后的每个项目都是随机的,每次都不同。
Plz给我正确的语法来获得回声
Mobile Number: 918888888888
User Name: We are unable to trace the Name for this Mobile Number
Mobile Operator Name: IDEA
State/Region: Maharashtra
其中 这些是ramdomly生成的,不同的evey时间因此,preg_match搜索这个loaction并回显同一个loaction中的文本
918888888888
We are unable to trace the Name for this Mobile Number
IDEA
Maharashtra
答案 0 :(得分:0)
这是XPath
的作品(参见[SimpleXMLElement::xpath
] [1],您的XPath看起来像是:
/form/table/tr[1]/td[2]/font[substring-after(./text(), ':') ->
/form/table/tr[2]/td[1]/font[1][substring-after(./text(), ':')
/form/table/tr[2]/td[1]/font[2][substring-before(substring-after(./text(), ':'), 'State')
/form/table/tr[2]/td[1]/font[2][substring-after(./text(), 'Region')
答案 1 :(得分:0)
$text = strip_tags($html);
我只是更改正则表达式并按(.*)
显示每个值匹配(跟随模式的所有内容一直到行尾:
preg_match('/Mobile Number: (.*)/', $html, $matches);
echo $matches[1];
preg_match('/User Name: (.*)/', $html, $matches);
echo $matches[1];
preg_match('/Mobile Operator Name: (.*)/', $html, $matches);
echo $matches[1];
preg_match('/State/Region: (.*)/', $html, $matches);
echo $matches[1];