PHP Simple HTML DOM Parser find string

前端 未结 4 766
心在旅途
心在旅途 2020-12-31 18:52

I am using PHP simple DOM parser but it does not seem to have the functionality to search for text. I need to search for a string and find the parent id for it. Essentially

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 19:30

    Just imagine that any tag has a "plaintext" attribute and use standart attribute selectors.

    So, HTML:

    London is the capital of Great Britain
    Washington is the capital of the USA

    can be imagined in mind as:

    London is the capital of Great Britain
    Washington is the capital of the USA

    And PHP to resolve your task is just:

    
          London is the capital of Great Britain
        
    Washington is the capital of the USA
    '; $html = str_get_html($t); $foo = $html->find('span[plaintext^=London]'); echo "ID: " . $foo[0]->parent()->id; // div1 ?>

    (take in mind that "plaintext" for tags is right-padded with a space symbol; this is default behaviour of Simple HTML DOM, defined by constant DEFAULT_SPAN_TEXT)

提交回复
热议问题