简体   繁体   中英

How to copy a particular line of text from HTML page via PHP?

I want to grab html code from a web page and I want to take some specific number of lines from that html code. I am able to fetch all information from webpage but cannot do copy. How can I do so?

My code is-

        $sourceURL = file($link);


        $title = '';

        foreach ($sourceURL as $value) {

            if(strpos($value, '<h1 class="title') !== false){

                //if it return true, wanna copy that line and store into $title variable.

            } else {
                echo "nothing";
            }
}

Any clue, how to do so?

You can use external library eg: http://simplehtmldom.sourceforge.net/

$html = new simple_html_dom();
$html->load_file($link);

$title = $html->find('h1[class=title]');

Thank you. I found the solution.

The best way to copy it, just use the value-

if(strpos($value, '<h1 class="title') !== false){

            $title = $value;

        } 

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