我试图确认一个特定的
以下是HTML的示例:
<div class="mgrRspnInline"> <div class="header"> David, Manager at The Pub, responded to this review </div> <p class="partial_entry"> <span id="response_232376288"> Thank you for taking the time to write a review and post feedback. We appreciate your comments, hope that you continue to visit us and be satisfied every time. Looking forward to seeing you again soon </span>
以下是我正在使用的代码:
$ret = $html->getElementByTagName('span');
//print_r($ret);
foreach($review_id as $value){
if($ret->find($value)){
echo "Yes";
} else {
echo "No";
}
}
$ review_id是数组IE中的审核ID列表 - response_232376288
,如果找到echo Yes
,我希望代码在此时执行response_232376288
而不是{{1}}没有,但我得到的只是不是。
如果有人可以提供帮助吗?
答案 0 :(得分:2)
是的,只需在循环内使用$value
作为您的针,即可搜索具有相应ID的跨度:
$html_string = '
<div class="mgrRspnInline">
<div class="header"> David, Manager at The Pub, responded to this review </div>
<p class="partial_entry">
<span id="response_232376288">
Thank you for taking the time to write a review and post feedback. We appreciate your comments, hope that you continue to visit us and be satisfied every time. Looking forward to seeing you again soon
</span>
</div>';
$html = str_get_html($html_string);
$review_id = array('response_232376288', 'response_99999999');
foreach($review_id as $value) {
echo 'The current value is: <strong>' . $value . '</strong><br/>';
echo 'Does it exist? <br/>';
$span = $html->find("span#$value", 0);
if($span != null) {
echo 'Yes!';
} else {
echo 'No :) sorry';
}
echo '<hr/>';
}
或者,我建议你可以在这种情况下使用DOMDocument
和xpath:
$html_string = '
<div class="mgrRspnInline">
<div class="header"> David, Manager at The Pub, responded to this review </div>
<p class="partial_entry">
<span id="response_232376288">
Thank you for taking the time to write a review and post feedback. We appreciate your comments, hope that you continue to visit us and be satisfied every time. Looking forward to seeing you again soon
</span>
</div>';
$dom = new DOMDocument();
$dom->loadHTML($html_string);
$xpath = new DOMXpath($dom);
$review_id = array('response_232376288', 'response_99999999');
foreach($review_id as $value) {
echo 'The current value is: <strong>' . $value . '</strong><br/>';
echo 'Does it exist? <br/>';
if($xpath->evaluate("count(//span[@id='$value'])") > 0) {
echo 'Yes!';
} else {
echo 'No :) sorry';
}
echo '<hr/>';
}
答案 1 :(得分:2)
让我们清理一下@ ghost的回答。答案很简单:
echo $html->find('#response_232376288', 0) ? 'Yes' : 'No';
真的没有必要用一堆额外的胡扯*来混淆这么简单的答案(除非你碰巧考虑额外的胡扯我想)