PHP 仅<p>从文本开始元素



am当前在我的网站上为 facebook Instant Article 工作,但没有显示内置图像,因为代表图像的<figure>元素必须在该图像中包含在内文章的主体,而不包含在<p>元素中。

$message="[img]http://img.loadedcamp.net/hfjfjd.jpg[/img]
This is the news and it is here.
[img]http://img.loadedcamp.net/YTgxbj.jpg[/img]
The new is posted by an author";

这是NL2P()函数

function nl2p($string, $only_if_no_html = FALSE) {
$replace = TRUE;
if ($only_if_no_html) {
$str2 = strip_tags($string);
if ($str2 != $string) {
  $replace = FALSE;
}
}
if ($replace) {
 return
  '<p>'
  .preg_replace('#(<brs*?/?>s*?){2,}#', '</p>'."n".'<p>', nl2br($string))
  .'</p>';
   }
}

此图像bbcode更改;

function savedimg($string) {
$string = preg_replace("#[img](.*?)[/img]#is", '<figure> <img src="$1"/> </figure>', $string);
return $string;
}

echo nl2p(savedimg($message));输出此;

<p><figure> <img src="http://img.loadedcamp.net/hfjfjd.jpg" /> </figure></p>
<p>This is the news and it is here.</p>
<p><figure> <img src="http://img.loadedcamp.net/YTgxbj.jpg" /> </figure></p>
<p>The new is posted by an author</p>

我希望p元素不在图像之间。请帮助伙计们!

这是我所期望的;

<figure> <img src="http://img.loadedcamp.net/hfjfjd.jpg" /> </figure>
<p>This is the news and it is here.</p>
<figure> <img src="http://img.loadedcamp.net/YTgxbj.jpg" /> </figure>
<p>The new is posted by an author</p>

savedimg函数中的正则表达式更改为以下:

...
$string = preg_replace("#(<p>s*?)?[img](.*?)[/img](s*?</p>)?#is", '<figure> <img src="$2"/> </figure>', $string );
...

最新更新