非有效标记上的简单html dom

时间:2013-07-12 13:48:20

标签: php html dom

我正在使用Simple HTML Dom,它在我的大部分数据上运行良好。然而,由于标记无效,因此很难。有没有其他方法可以在PHP中执行此操作。

我从一个页面得到了这个结果,我试图从中提取价格:

<taconite><replacecontent select="#basketcontents"><![CDATA[
                <table id="sellingb" cellpadding="10px" cellspacing="15" width="600" border="0">
                  <thead>
                    <tr class="title">
                      <th width="47" scope="col" align="center">Book</th>
                      <th width="213" scope="col" align="left">Title</th>
                      <th width="139" scope="col" align="left">ISBN/Barcode</th>
                      <th width="63" scope="col" align="left">Value</th>
                      <th width="29" scope="col">&nbsp;</th>
                    </tr>
                  </thead>
                  <tbody>
                              <tr class="trrow">
                                <td class="tdbook" align="center" valign="middle" ><img src="http://ecx.images-amazon.com/images/I/61JEp-wF3zL._SL75_.jpg" /><input name="offers_row_img[0]" type="hidden" value="http://ecx.images-amazon.com/images/I/61JEp-wF3zL._SL75_.jpg" /></td>
                                <td class="tdtitle">The Last Of Us (PS3) [Video Games]<input name="offers_row_title[0]" type="hidden" value="The Last Of Us (PS3) [Video Games]" /></td>
                                <td class="tdisbn">0711719274551<input name="offers_row_isbn[0]" type="hidden" value="0711719274551" /></td>
                                <td class="tdval">£15.00<input name="offers_row_price[0]" type="hidden" value="15.00" /></td>
                                <td class="tdremove"><input type="button" onclick="removeitem(0);" value="Reject Offer" /></td>
                              </tr>

                  </tbody>
                  </table>]]></replacecontent><eval><![CDATA[jQuery('#isbn').val('');]]></eval><replacecontent select="#price"><![CDATA[£15.00<br /><input type="button" class="bask-sb" id="acceptoffer" onclick="confirm('By clicking OK you are accepting the offer of £15.00 for your 1 item(s).'); acceptoffer();"/>]]></replacecontent></taconite>

然而,似乎存在问题。简单的HTML Dom仅适用于有效标记,这是无效的。什么是我从这个结果中提取15英镑的最佳方式。

感谢。非常感谢。

1 个答案:

答案 0 :(得分:0)

仅使用有效标记,除非标记由其他资源使用。

一种替代方法可能是使用strpos() / substr(),例如:

$price = substr($input,strpos($h, "$")); // or euro symbol whatever you need
$price = substr($x, 0, strpos($x, "<")); 

我假设您的输入设置为变量$ input。

只有在您确定价格之后的下一个字符为<并且只有一个价格实例时才能正常运行。如果有多个价格实例,您将不得不调整它以获得正确的价格实例。