Search code examples
htmlsimple-html-dom

How can i parse what i want with this HTML code?


I would like to extract the attributes "href" and "title" from this HTML code :

<span class='ipsType_break ipsContained'>
  <a 
    href='https://www.xxxx/topic/11604/' 
    class='' title='[2002] Le Boulet [xxxx] '
    data-ipsHover data-ipsHover-target='https://www.xxxxx/topic/1160'
    data-ipsHover-timeout='1.5'>
<span>[2002] Le Boulet [xxxxx]</span>
  </a>
</span>

I tried some codes in PHP but it's not working :(

By example

$e = $html->find('span[class=ipsType_break ipsContained]');
$value = $e->title;
print_r($value);

Solution

  • You can use a single call using find, but you have to indicate that you are looking for an anchor a, as the span here does not have a title attribute.

    As find returns an array, you have to indicate that you want the first element by specifying 0 as the second argument.

    $e = $html->find('span[class=ipsType_break ipsContained] a', 0);
    echo $e->href . PHP_EOL;
    echo $e->title;
    

    Output

    https://www.xxxx/topic/11604/
    [2002] Le Boulet [xxxx]