PHP のサーバー側
非常に基本的な方法は次のようになります。
$data = ''; // your HTML data from the question
preg_match( '/<div class="\st\">(.*?)<\/div>/', $data, $match );
次に、オブジェクトを繰り返し$match
ます。ただし、.st
DIV の内部に別の DIV がある場合、これは不正なデータを返す可能性があります。
より適切な方法は次のとおりです。
function getData()
{
$dom = new DOMDocument;
$dom -> loadHTML( $data );
$divs = $dom -> getElementsByTagName('div');
foreach ( $divs as $div )
{
if ( $div -> hasAttribute('class') && strpos( $div -> getAttribute('class'), 'st' ) !== false )
{
return $div -> nodeValue;
}
}
}
クライアント側
jQueryを使用している場合は、次のように簡単です。
$('.st').text();
また
$('.st').html();
プレーンな JavaScript を使用している場合、目的の CSS クラスを持つ要素が見つかるまで、すべての DIV 要素をチェックする必要があるため、少し複雑になります。
function foo()
{
var divs = document.getElementsByTagName('div'), i;
for (i in divs)
{
if (divs[i].className.indexOf('st') > -1)
{
return divs[i].innerHTML;
}
}
}