我正在尝试将mySQL数据库中的HTML标签从纯文本中删除为SEO元描述。数据库中的HTML如下所示:
<p>The Break-ezee is a vital piece of equipment for anyone when breaking horses - As used by Mary King.</p>
<p></p>
<p>The Break-ezee is an all in one progressive training product for use when breaking horses.</p>
我使用以下PHP格式化它:
$showseodesc = trim(strip_tags(substr($showseoproduct['text'],0,160)));
这在网站的来源中显示以下内容:
<meta name="description" content="The Break-ezee is a vital piece of equipment for anyone when breaking horses - As used by Mary King.
The Break-ezee is an all in one progressi" />
无论如何我可以替换任何标签(在这种情况下&lt; p&gt;)所以没有空格?
我理想地寻找Meta描述如下:
<meta name="description" content="The Break-ezee is a vital piece of equipment for anyone when breaking horses - As used by Mary King. The Break-ezee is an all in one progressi" />
另外,我认为谷歌没有为元描述获取额外的空间吗?
非常感谢你的帮助。
答案 0 :(得分:1)
您可以使用str_replace
$showseodesc = str_replace(array('<p>', '</p>'), '', $showseodesc);
$showseodec = substr($showseoproduct['text'],0, 160);
答案 1 :(得分:0)
尝试使用正则表达式。
$string = "<p>The Break-ezee is a vital piece of equipment for anyone when breaking horses - As used by Mary King.</p>";
print preg_replace("|<[^>]+>|si","",$string); // <-- strip all tags from a string.
print preg_replace("|<p[^>]*>|si","",$string); // <-- strip all <p...> tags from a string.
答案 2 :(得分:0)
试试这个:
$showseodesc = trim(
preg_replace("/\n+/"," " ,
strip_tags(
substr($showseoproduct['text'], 0, 160)
)
)
);
注意preg_replace
,将每个换行符更改为一个空格。
答案 3 :(得分:0)
str_replace就足够了: -
$html = "<p>The Break-ezee is a vital piece of equipment for anyone when breaking horses - As used by Mary King.</p>
<p></p>
<p>The Break-ezee is an all in one progressive training product for use when breaking horses.</p>";
echo str_replace(array('<p>', '</p>'), '', $html);
输出: -
Break-ezee对于任何打马的人来说都是至关重要的设备 - 正如Mary King所使用的那样。 Break-ezee是一种用于打马时的一体化进步训练产品。