単語を切り刻むことなく文字列をいくつかの文字にカットするこの関数があります。ページのmeta_descriptionに使用します。
function meta_description_function($text, $length){ // Meta Description Function
if(strlen($text) > $length) {
$text = substr($text, 0, strpos($text, ' ', $length));
}
return $text;
}
私はワードプレスの投稿からコンテンツを引き出します:
$content = $content_post->post_content;
コンテンツからタグを削除します。
$content = strip_tags($content);
そして、私は自分の機能をコンテンツに適用します:
$meta_description = meta_description_function($content, 140);
問題は、$contentが次のような場合です。
<p>Hello I am sentence one inside my own paragraph.</p>
<p>Hello I am sentence two inside my own paragraph.</p>
コンテンツを適用して$meta_descriptionをエコーした後、次のようなさまざまな行の文を取得します。
<meta name="description" content="Hello I am sentence one inside my own paragraph.
**<i get a space here!>**
Hello I am sentence two inside my own paragraph." />
ストリップタグを使用した場合、なぜこの空のスペースが表示されるのですか?それを非表示にするにはどうすればよいですか?