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

时间:2017-02-15 18:09:52

标签: php preg-replace facebook-instant-articles

我目前正在为我的网站工作 facebook即时文章,但是因为代表图像的['W', 'b', 'input']元素必须单独包含在文章正文并未包含在<figure>元素中。

<p>

这是nl2p()函数

$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";

此图片bbcode更换器;

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('#(<br\s*?/?>\s*?){2,}#', '</p>'."\n".'<p>', nl2br($string))
  .'</p>';
   }
}

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

1 个答案:

答案 0 :(得分:0)

savedimg功能中的正则表达式模式更改为以下内容:

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