在数组中回显超链接

echo hyperlinks in array

我有一个如下所示的数组:

   [6625] => Trump class="mediatype"> href="/news/picture">Slideshow: [6628] => href="http://www.example.com/news/picture/god=USRTX1N84J">GOP [6630] => nation 

我需要能够从数组的 href="" 中取出任何内容并放入一个新数组中。

我试过:

      <?php
$homepage = file_get_contents('http://www.example.com/');
$arr = explode(" ",$homepage);


function getStringInBetween($string, $start, $end){
    $string = " " . $string;
    $initial = strpos($string, $start);

    if ($initial == 0) return "";

    $initial += strlen($start);
    $length = strpos($string, $end, $initial) - $initial;

    return substr($string, $initial, $length);
}

echo getStringInBetween($arr[0], 'href="', '"')
?>

试试这个代码,让它适合你,

<?php
$homepage = file_get_contents('http://www.example.com/');
$arr = explode(" ",$homepage);

function getStringInBetween($string, $start, $end){
    $string = " " . $string;
    $initial = strpos($string, $start);

    if ($initial == 0) return "";

    $initial += strlen($start);
    $length = strpos($string, $end, $initial) - $initial;

    return substr($string, $initial, $length);
}

foreach ($arr as $val) {
    if (strpos($val, 'href') !== false) {
        echo getStringInBetween($val, 'href="', '"');
    }
}
?>

这个例子当运行输出google.com/hello.