The code below searches WordPress content for words and replaces these words with a link and a div. It creates a problem where the div closes the <p> tag it was inserted into.
$myposts = get_pages(args...);
$replace = array();
$i = 1;
foreach( $myposts as $post ) {
setup_postdata($post);
$replace[get_the_title()] = '<a href="#popupBasic' . $i . '" data-rel="popup">' . get_the_title() . '</a><div data-role="popup" class="tooltipBox" id="popupBasic' . $i . '">' . get_the_content() . '</div>';
$i++;
}
$text = str_replace(array_keys($replace), $replace, $text);
Why is the str_replace breaking the <p> tag like this?
<div>
<p>Some random text thats been <a href="#" class="link">cutt</a></p><div style="display: none;"><!-- placeholder --></div> off.<p></p>
</div>
If it cannot be fixed within PHP, can we fix this with jQuery?
I want to put back the word thats been cut off, in this case ” off.” into the <p> tag that is closed after the link. And also remove the empty <p> tag in the end.
pelements contain phrasing content.divis not valid in phrasing content, it’s valid in flow content. (Putting adivinside apis like putting apinside ap.) So the browser is making the best of invalid markup.Try using a
spanrather than adiv.