Get the inner text using curl concept in php

前端 未结 5 1521
情深已故
情深已故 2021-01-25 15:18

This is html text in the website, i want to grab

1,000 Places To See Before You Die

相关标签:
5条回答
  • 2021-01-25 15:37

    There are 2 ways that I could think of to solve this. One, is that you grab the title attribute from the anchor tag. Of course, not everyone set up a title attribute for the anchor tag and the value of the attribute could be different if they want to fill it that way. The other solution is that, you get the innertext attribute and then replace every child of the anchor tag with an empty value.

    So, either do this

    $e->title;
    

    or this

    $text = $e->innertext;
    foreach ($e->children() as $child)
    {
        $text = str_replace($child, '', $text);
    }
    

    Though, it might be a good idea to use DOMDocument instead for this.

    0 讨论(0)
  • 2021-01-25 15:39

    First of all check your html. Now it is like

      $string = '<ul class="listings">
                   <li>
                      <a href="http://watchseries.eu/serie/1,000_places_to_see_before_you_die" title="1,000 Places To See Before You Die">
     1,000 Places To See Before You Die
                        <span class="epnum">2009</span>
                     </a>
                 </li>';
    

    There is no close tag for ul, perhaps you missed it.

      $string = '<ul class="listings">
                   <li>
                      <a href="http://watchseries.eu/serie/1,000_places_to_see_before_you_die" title="1,000 Places To See Before You Die">
     1,000 Places To See Before You Die
                        <span class="epnum">2009</span>
                     </a>
                 </li>
                </ul>';
    

    Try like this

     $xml = simplexml_load_string($string);
     echo $xml->li->a['title'];
    
    0 讨论(0)
  • 2021-01-25 15:44

    Why not DOMDocument and get title attribute?:

    $string = '<ul class="listings">
    <li>
    <a href="http://watchseries.eu/serie/1,000_places_to_see_before_you_die" title="1,000 Places To See Before You Die">
    1,000 Places To See Before You Die
    <span class="epnum">2009</span>
    </a>
    </li>';
    
    $dom = new DOMDocument;
    $dom->loadHTML($string);
    $xpath = new DOMXPath($dom);
    $text = $xpath->query('//ul[@class="listings"]/li/a/@title')->item(0)->nodeValue;
    echo $text;
    

    or

    $text = explode("\n", trim($xpath->query('//ul[@class="listings"]/li/a')->item(0)->nodeValue));
    echo $text[0];
    

    Codepad Example

    0 讨论(0)
  • 2021-01-25 15:52

    You can use strip_tags() for that

    echo trim(strip_tags($e->innertext));
    

    Or try to use preg_replace() to remove unwanted tag and its content

    echo preg_replace('/<span[^>]*>([\s\S]*?)<\/span[^>]*>/', '', $e->innertext);
    
    0 讨论(0)
  • 2021-01-25 16:00

    Use plaintext instead.

    echo $e->plaintext;
    

    But still the year will be present which you can trim off using regexp.

    Example from the documentation here:

    $html = str_get_html("<div>foo <b>bar</b></div>");
    $e = $html->find("div", 0);
    
    echo $e->tag; // Returns: " div"
    echo $e->outertext; // Returns: " <div>foo <b>bar</b></div>"
    echo $e->innertext; // Returns: " foo <b>bar</b>"
    echo $e->plaintext; // Returns: " foo bar"
    
    0 讨论(0)
提交回复
热议问题