PHP 仅从文本开始 <p> 元素
PHP begin <p> element from text only
我目前正在为我的网站制作 facebook 即时文章,但是 in-post 图像没有显示,因为代表图像的 <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('#(<br\s*?/?>\s*?){2,}#', '</p>'."\n".'<p>', nl2br($string))
.'</p>';
}
}
此图像 bbcode 转换器;
function savedimg($string) {
$string = preg_replace("#\[img\](.*?)\[/img\]#is", '<figure> <img src=""/> </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=""/> </figure>', $string );
...
我目前正在为我的网站制作 facebook 即时文章,但是 in-post 图像没有显示,因为代表图像的 <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('#(<br\s*?/?>\s*?){2,}#', '</p>'."\n".'<p>', nl2br($string))
.'</p>';
}
}
此图像 bbcode 转换器;
function savedimg($string) {
$string = preg_replace("#\[img\](.*?)\[/img\]#is", '<figure> <img src=""/> </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=""/> </figure>', $string );
...