Modify the URL of a href using PHP HTML DOM

0

I want to modify a URL of an object that I am extracting from another site, in this case, from Exame.com.

Follow the code:

 $exame = file_get_html("http://exame.abril.com.br/");
                $exame_posts = $exame->find("p.content-item-title");
                foreach($exame_posts as $i => $value){  
                    if($i < 10){
                    echo $value;
                    }

                }

The code is simple, it takes the "p" tags with the "content-item-title" class and limits to 10 and prints the 10.

The question is: how do I modify the url of some of these links that I get? Some links I can access the website of Exame.com, others (few) do not. I'll show an example of how some links are:

The links as they are generated in the HTML page:

HowdoestheurlgetwhenIclickononeofthem:

And after the url is correct, how to avoid a possible conflict on those other links that are without error?

Hugs!

    
asked by anonymous 23.09.2016 / 01:36

1 answer

0

I think you can do this by using str_ireplace in $value before giving echo :

 $site = "http://exame.abril.com.br/";
 $exame = file_get_html($site);
                $exame_posts = $exame->find("p.content-item-title");
               foreach($exame_posts as $i => $value){  
                    if($i < 10){
                    if (!preg_match('/(href="http|href=\'http)/i', $value)) {
                        $value = str_ireplace(array('href="', "href='", $site.'/'), array('href="'.$site, "href='".$site, $site), $value);
                    }
                    echo $value;
                    }

                }       
    
23.09.2016 / 13:26