2016-05-23 6 views
0

ファイルを取得して目的のページ内の特定の要素のみを取得することができますか?たとえば、<title></title>タグ内または<meta></meta>タグ内にのみコンテンツを格納します。 file_get_contentsで実現できないことがあれば、それを達成するために何が使えますか?PHPファイルを取得する

ありがとうございます!

答えて

1

explode()機能を使用できます。

$content = file_get_contents("http://www.demo.com"); 
$explodedContent = explode("<title>", $content); 
$explodedExplodedContent = explode("</title>", $explodedContent[1]); 

echo $explodedExplodedContent[0]; // title of that page. 

あなたは関数にそれを作ることができます。

function contentBetween($beginStr, $endStr, $contentURL){ 
    $content = file_get_contents($contentURL); 
    $explodedContent = explode($beginStr, $content); 
    $explodedExplodedContent = explode($endStr, $explodedContent[1]); 

    return $explodedExplodedContent[0]; 
} 

echo contentBetween("<title>", "</title>", "http://www.demo.com"); // usage. 

explode() Fの詳細を参照してください:http://php.net/manual/tr/function.explode.php

+0

賢いが、ボックスの外側を考えると、私はそれが好き! –

+0

私は同意します!これは素晴らしいです、それはあまりにも動作します!私はちょうど1つの質問を持っています - 私が次のようにすると、実際に一致しようとしている文字列の数が返ってこないのです: 'echo substr_count($ explodedExplodedContent [0]、 'Solving'); ' 私は '21'を返します。期待した '1'ではありません。 –

+0

これは面白いです、あなたは完全なコードを共有することができますか? –

関連する問題