2
<p style="color:red;font-size:12px;">This economy car is great value for money and with the added benefit of air conditioning is ideal for couples and small families. A ?500 excess applies which can be waived to NIL for only <b>5.00</b> per day</p>

以下の2つの方法を使用します

substr($mytext,0,25);

 $s = html_entity_decode($mytext);
 $sub = substr($s, 0, 50);�

最初の50文字を取得する必要があります...誰か助けてください

ありがとう

4

2 に答える 2

2

HTMLペーサーが必要です。プレーンテキストを見つけて読み取り、部分文字列を選択します。ここでは、次の例を示しDOMXpathます。

$doc = DOMDocument::loadHTML($html);
$xp = new DOMXPath($doc);
$chars50 = $xp->evaluate('substring(normalize-space(//body),1,50)');

デモ

文字列(50)"This economy car is great value for money and with"

ここでUTF-8でエンコードされた文字列を取得することに注意してください。たとえば、正規表現(単語を切り取るのに役立つ場合があります)を使用して、これを自分で行うこともできます。

# load text from HTML
$text = DOMDocument::loadHTML($html)->getElementsByTagName('body')->item(0)->nodeValue;

# normalize HTML whitspace
$text = trim(preg_replace('/\s{1,}/u', ' ', $text));

# obtain the substring (here: UTF-8 safe operation, see as well mb_substr)
$chars50 = preg_replace('/^(.{0,50}).*$/u', '$1', $text);

デモ

HTMLパーサーの代わりに使用している場合はstrip_tags、さまざまなエンコーディングを自分で処理する必要があります。元の文字列にはすでにUnicode置換文字を示す疑問符が付いているので、すでに中断されたデータを処理していると思います。安全ではないDOMDocument代わりに、のように再表示するライブラリを使用することをお勧めしstrip_tagsます(PHPの警告を参照してください)。マニュアルページ)。

于 2012-04-18T13:55:10.547 に答える
1

これが機能していることを願っています...試してみてください

echo (substr(strip_tags($mytext), 0, 25));

http://www.ideone.com/6TgJX

于 2012-04-18T13:43:07.747 に答える