Remove triple line breaks from Hugin online
In a feed from Hugin Online a customer added several line breaks, with a special comment, so it should look nice in the newsletter, but that didn't look nice on the site. So I had to remove certain line breakers but not all. So I explode the string into an array by the separator
<br> and restored them if the one before was the same, as is line break ASCII 13.
<?php
/**
* Remove triple line breaks
*
* @param unknown_type $body
*/
function remove_3brake($body){
$body = explode('<!-- hugin-supplied --><br>', $body);
foreach ($body as $key => $value) {
if ($body[$key-1] == $value) {
}else{
$out[] = $value;
}
}
$out = implode("<br>", $out);
//print_r($out) ;
return $out;
}
?>Or use a nice library called PHP Simple HTML DOM Parser
<?php
/**
* Hugin:
* Remove all brakes after two hugin-supplied line brakes
* Remove all hugin tags and empty linebreaks
* Remove a not disired paste tag
*
*
*/
function mymodule_body_filter($string, $brakes = 2){
require_once('sites/default/libraries/simplehtmldom/simple_html_dom.php');
$html = str_get_html($string);
foreach($html->find(0) as $hugin) {
/************* Apsis RSS tag **********/
if ($hugin->tag == "comment") {
if($hugin->innertext == "<!-- hugin-supplied -->"){
$hugin->outertext = '';
$comment = TRUE;
}
}
if ($comment == TRUE AND $hugin->tag == "br") {
$break_found++;
}
if ($break_found > $brakes) {
if ($hugin->tag == "br") {
$hugin->outertext = '';
//4 chars => \n\r
}elseif (strlen($hugin->plaintext) == 4 AND $hugin->tag == "text") {
$hugin->outertext = '';
}
}
if(strlen($hugin->plaintext) > 4 AND $hugin->tag == "text"){
unset($comment, $break_found);
}
/************* end Apsis RSS tag *******/
/************* copy paste tag **********/
if($hugin->tag == "comment" AND $hugin->innertext == "<!--EndFragment-->"){
$hugin->outertext = '';
}
/************* end copy paste tag ******/
}
return $html->save();
}
?>