tisuchi
tisuchi

Reputation: 954

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?

Upvotes: 0

Views: 261

Answers (2)

tisuchi
tisuchi

Reputation: 954

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;

        } 

Upvotes: 0

semafor
semafor

Reputation: 81

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

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

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

Upvotes: 2

Related Questions