I have a string that shows each post then a <hr>
element at the end of a post. But I have it so if you delete a post it shows leaves the <hr>
element there so I need a script that will detect if there is more than one <hr>
element and narrow it down so that it will only be left with one below each post.
Is there any chance that it is possible to do???
here is what it looks like to me:
post text post text post text post text post text
<hr>
post text post text post text post text post text post text
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
post text post text post text
<hr>
<hr>
post text post text post text post text
<hr>
And I need it to be like:
post text post text post text post text post text
<hr>
post text post text post text post text post text post text
<hr>
post text post text post text
<hr>
post text post text post text post text
<hr>
here<hr>
preg_replace()
PHP:
<?php
$content = <<<STR
post text post text post text post text post textA
<hr>
post text post text post text post text post text post textB
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
post text post text post textC
<hr>
<hr>
post text post text post text post textD
STR;
function remove_duplicate_hr_tags($string)
{
$lines = explode('<hr>', $string);
$trimmedArray = array_map('trim', $lines);
$emptyRemovedArray = array_filter($trimmedArray);
$content = implode("
<hr>
", $emptyRemovedArray);
return $content;
}
echo remove_duplicate_hr_tags($content);
Output
post text post text post text post text post textA
<hr>
post text post text post text post text post text post textB
<hr>
post text post text post textC
<hr>
post text post text post text post textD
Test this regex:
$str = preg_replace('/(<hr>[
\s]{0,}<hr>[
\s]{0,}){1,}/', '<hr>', $str);
echo $str;
See this live running code : https://eval.in/521981
could have been more helpful if you provide code.
my suggestion would be group them in div .
<div class="container">
<p>post 1 text</p>
<hr>
</div>
<div class="container">
<p>post 2 text</p>
<hr>
</div>