Am currently working of facebook instant article for my website but the in-post images are not showing because the <figure>
element representing the image must be included standalone within the body of the article and not enclosed within a <p>
element.
$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";
This is nl2p() function
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>';
}
}
This image bbcode changer;
function savedimg($string) {
$string = preg_replace("#\[img\](.*?)\[/img\]#is", '<figure> <img src="$1"/> </figure>', $string);
return $string;
}
echo nl2p(savedimg($message));
outputted this;
<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>
I want the p
element not to be in-between the images. Please help guys!
This is what i expected as output;
<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>
Change the regex pattern in your savedimg
function to the following:
...
$string = preg_replace("#(<p>\s*?)?\[img\](.*?)\[/img\](\s*?</p>)?#is", '<figure> <img src="$2"/> </figure>', $string );
...