简体   繁体   中英

PHP Simple HTML DOM Parser - Find Span ID

I am trying to confirm if a particular

Below is an example of the 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>

And below is the code I am using:

$ret = $html->getElementByTagName('span');
//print_r($ret);
foreach($review_id as $value){
    if($ret->find($value)){
        echo "Yes"; 
    } else {
        echo "No";  
    }
}

$review_id is a list of review IDs in an array IE - response_232376288 , all I want the code to do at this point is echo Yes if it finds response_232376288 and no if it does not, but all I am getting is No's.

If anyone can assist?

Yes, just use that $value as your needle while inside the loop to search for that span with that coresponding 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/>';
}

Alternatively, and I suggest you can use DOMDocument with xpath in this case:

$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/>';
}

Sample Demo

Let's clean @ghost's answer up a bit. The answer is as simple as:

echo $html->find('#response_232376288', 0) ? 'Yes' : 'No';

There's really no need to clutter such a simple answer up with a bunch of extra baloney *(unless you happen to care for extra baloney I suppose)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM