PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
$new_str =
"
<a href="http://www1.folha.uol.com.br/folha/brasil/ult96u49003.shtml">Link 1</a><br>
<a href="http://www1.folha.uol.com.br/folha/mundo/ult94u56767.shtml">Link 2</a><br>
<a href="http://www1.folha.uol.com.br/folha/dinheiro/ult91u67128.shtml">Link 3</a><br>
<a href="http://www1.folha.uol.com.br/folha/cotidiano/ult95u74774.shtml">Link 4</a><br>
<a href="http://www1.folha.uol.com.br/folha/esporte/ult92u59688.shtml">Link 5</a><br>
<a href="http://www1.folha.uol.com.br/folha/ilustrada/ult90u32944.shtml">Link 6</a><br>
<a href="http://www1.folha.uol.com.br/folha/informatica/ult124u12907.shtml">Link 7</a><br>
<a href="http://www1.folha.uol.com.br/folha/ciencia/ult306u9093.shtml">Link 8</a><br>
<a href="http://www1.folha.uol.com.br/folha/educacao/ult305u12840.shtml">Link 9</a>
";
//Let's say I don't know how many links the string has, so I do this
$abc = preg_match_all("/<a.+?hrefs*=s*([']?)(.+?)*?>/is", $new_str, $match_l);
for($aa=0;$aa<=$abc;$aa++) {
foreach($match_l as $value) {
//Here I extract the "junk" to have only the URL
$str_url = eregi_replace(""","",$value[$aa]);
$str_url = eregi_replace("<a href=","",$str_url);
$str_url = eregi_replace(">","",$str_url);
print "$str_url<br>";
//Here I try to open the URL to get it's content
if(!($File=fopen($str_url,"r")))
{
echo "There was a problem fetching the content requested.";
exit;
}
while(!feof($File))
{
$Line.=fgets($File,2000055);
}
fclose($File);
$startN="<!--NOTICIA-->";
$endN="<!--/NOTICIA-->";
$start_positionN=strpos($Line, $startN);
$end_positionN=strpos($Line, $endN)+strlen($endN);
$lengthN=$end_positionN-$start_positionN;
$Line=substr($Line, $start_positionN, $lengthN);
echo "<br><br>$Line<BR><BR>";
}
}
?>
The problem is that it only get the first URL's content, when it tries to move on to the next URL, it stops.
What I can't understand is why the $str_url inside the foreach loop, which is inside the for loop, is not passing the values after the first loop. I can't see anything wrong in the code.
<?php
$new_str =
"
<a href="http://www1.folha.uol.com.br/folha/brasil/ult96u49003.shtml">Link 1</a><br>
<a href="http://www1.folha.uol.com.br/folha/mundo/ult94u56767.shtml">Link 2</a><br>
<a href="http://www1.folha.uol.com.br/folha/dinheiro/ult91u67128.shtml">Link 3</a><br>
<a href="http://www1.folha.uol.com.br/folha/cotidiano/ult95u74774.shtml">Link 4</a><br>
<a href="http://www1.folha.uol.com.br/folha/esporte/ult92u59688.shtml">Link 5</a><br>
<a href="http://www1.folha.uol.com.br/folha/ilustrada/ult90u32944.shtml">Link 6</a><br>
<a href="http://www1.folha.uol.com.br/folha/informatica/ult124u12907.shtml">Link 7</a><br>
<a href="http://www1.folha.uol.com.br/folha/ciencia/ult306u9093.shtml">Link 8</a><br>
<a href="http://www1.folha.uol.com.br/folha/educacao/ult305u12840.shtml">Link 9</a>
";
//Let's say I don't know how many links the string has, so I do this
$abc = preg_match_all("/<a.+?hrefs*=s*([']?)(.+?)*?>/is", $new_str, $match_l);
// $match_l[0] is the array of all "full pattern matches, all other $match_l[N] should have the same amount of elements
foreach($match_l[0] as $key=>$wholematch)
{
echo '<fieldset><legend>match #', $key, '</legend>';
foreach($match_l as $pos=>$field)
echo 'substring #', $pos, '=', htmlentities($field[$key]), '<br />'; // the same as $match[$pos][$key]
echo '</fieldset>';
}
?>