0

たとえば、htmlで内部リンクを見つけようとしています

<p>This is first <a href="/old-link-first">old link</a> and this is second <a href="/old-link-second">old link</a></p>

データベースからリンクを取得するには、データベースで相対リンクを照合する必要があります。:/ の照合と実行に問題があります。

私はこれを試しましたが、最初のリンクでのみ機能するようです:

$out='<p>This is first <a href="/old-link-first">old link</a> and this is second <a href="/old-link-second">old link</a></p>';

$pattern = '/href="\/(.*)?"/';

function ch($match){
    $oldurl = 'href="'.$match[1].'"';
    // query to database
    return $newurl;
}

$out = preg_replace_callback($pattern, 'ch', $out);
print $out;

間違いが多いと思います よろしくお願いします

4

1 に答える 1