0 レビュー
1 回答
html-PHPを使用してテキストのブロックからハイパーリンクを切り抜く
ウェブページに次のHTMLがあります。
<p>This is a <a href="http://www.google.com/">hyperlink</a> and this is another <a href="http://www.bing.com/">hyperlink</a>. There are many like it, but <a href="http://en.wikipedia.org/wiki/Full_Metal_Jacket">this one is mine</a>.</p>
今、私は疑問に思っていました...
PHP関数を使用して、このテキストブロックを配列に分割する方法はありますか?
$html[0] = "<p>This is a & this is another . There are many like it, but .</p>";
$html[1] = "http://www.google.com/";
$html[2] = "http://www.bing.com/";
$html[3] = "http://en.wikipedia.org/wiki/Full_Metal_Jacket";
つまり、基本的に、すべてのハイパーリンクのテキストの最初のブロックを取り除き、それらをすべて独自の配列要素に格納します。
これについて助けてくれてありがとう。
わからない
0
レビュー
答え :
解決策:
この正規表現を使用してhtmlのURLを取得します:
$url = "http://www.example.net/somepage.html";
$input = @file_get_contents($url) or die("Could not access file: $url");
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $input, $matches)) {
// $matches[2] = array of link addresses
// $matches[3] = array of link text - including HTML code
}
?>
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。