开发者

how do i reverse the order when echoing my results?

开发者 https://www.devze.com 2023-02-04 00:45 出处:网络
i have this bit of code: <?php $file = file_get_contents(\'http://example.com\'); preg_match_all(\"/<a href=(.*?links.*?)>.*?<\\/a>/i\", $file, $a);

i have this bit of code:

<?php   
$file = file_get_contents('http://example.com');
preg_match_all("/<a href=(.*?links.*?)>.*?<\/a>/i", $file, $a);
$count = count($a[1]);
for ($row = 0; $row < $count ; $row++) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
?> 

and currently it echos the links in order they appear on the website. I wou开发者_Python百科ld like for it to echo the results in a reverse order (the last link on example.com to echo as the first with this code)

any help is appreciated! thanks.


for ($row = $count - 1; $row > -1 ; $row--) {
    $linkurls = $a[1]["$row"];
    echo ' '.$linkurls.' <br>';
}


PHP has a function for it: array_reverse.

$a[1] = array_reverse($a[1]);

Also why would you use ["$row"] instead of [$row]? There is no functional difference as numeric strings get converted back to numbers when using them as indexes, so don't worry about that, but just because something can be done doesn't mean you should do it.


<?php   
$file = file_get_contents('http://example.com');
preg_match_all("/<a href=(.*?links.*?)>.*?<\/a>/i", $file, $a);
$a = array_reverse($a);
$count = count($a[1]);
for ($row = 0; $row < $count ; $row++) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
?> 
0

精彩评论

暂无评论...
验证码 换一张
取 消