0

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?

2 Answers 2

2

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]');
3
  • 1
    Thank you.. I found the solution already. By the way, I can't use external all libraries becoz this code is going to use Laravel.
    – tisuchi
    Jun 18, 2016 at 12:49
  • 2
    If you use Laravel then you can just use Bundle for HTML Dom e.g symfony.com/doc/current/components/dom_crawler.html
    – semafor
    Jun 18, 2016 at 12:55
  • 1
    thats really nice package. Let's give a try. TQVM
    – tisuchi
    Jun 18, 2016 at 13:05
0

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;

        } 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.