0

我正在尝试将标签从 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" />

无论如何我可以替换任何标签(在本例中为 <p>)所以没有空格?

理想情况下,我希望元描述看起来像这样:

<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" />

另外,我认为谷歌没有为元描述选择额外的空间是正确的吗?

非常感谢您的帮助。

4

4 回答 4

1

你可以使用str_replace

$showseodesc = str_replace(array('<p>', '</p>'), '', $showseodesc);

$showseodec = substr($showseoproduct['text'],0, 160);

于 2013-06-12T10:28:22.407 回答
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.
于 2013-06-12T10:29:50.337 回答
0

尝试这个:

$showseodesc = trim(
                   preg_replace("/\n+/"," " , 
                               strip_tags(
                                         substr($showseoproduct['text'], 0, 160)
                                         )
                               )
                   );

请注意preg_replace,这会将每个换行符更改为一个空格。

于 2013-06-12T10:31:05.507 回答
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 是一款多合一的渐进式训练产品,可在打破马匹时使用。

于 2013-06-12T10:35:43.223 回答