Replace all paragraphs containing keyword

Hello,

I have to replace all paragraphs containing keyword with a blank, while leaving all other paragraphs intact.

something of this kind:

$line .= preg_replace (
                '\\r\
(.*)Keyword(.*)\\r\
',     
                '  ',   
                $line);

I don’t know if this following example fits for you or not but it seems that it works just fine:


$string = 'Morbi id sem ut lacus eleifend venenatis ut nec sapien. 
Vestibulum placerat diam quis eros rutrum nec commodo magna viverra. Vivamus cursus enim quis libero imperdiet porttitor. 
Praesent suscipit, ipsum eget eleifend egestas, odio tellus congue dui, vel viverra nulla nisl eleifend enim. Nunc tellus ipsum, cursus in auctor vel, cursus id risus. 

Morbi mi libero, consequat vitae eleifend ac, porttitor tempor quam. Duis venenatis pellentesque felis sodales mollis. Suspendisse potenti. 

Sed mattis scelerisque imperdiet. Quisque feugiat tortor ac velit consequat vestibulum. Maecenas vitae eros sed ante interdum rhoncus. 
Phasellus metus consequat, ultricies eu viverra eu, mollis quis purus. In hac habitasse platea dictumst. Sed quam erat, rutrum nec sodales nec, consequat porta felis. 

Suspendisse tincidunt cursus diam eget pulvinar. Aliquam sagittis molestie urna. Suspendisse potenti. Fusce quis metus turpis. Phasellus aliquet gravida nunc, quis malesuada metus venenatis nec. 

Pellentesque congue neque eu odio condimentum interdum.';
echo preg_replace ('/\\r\
|\
.*?consequat.*?\\r\
|\
/is', '####', $string);


echo preg_replace ('/\\r\
|\
.*?consequat.*?\\r\
|\
/is', '####', $string);

It won’t work because your regex says:


"\\r\
" or "\
.*?consequat.*?\\r\
" or "\
"

You probably meant:


echo preg_replace ('/(?:\\r\
|\
).*?consequat.*?(?:\\r\
|\
)/is', '####', $string);

Which still is wrong because it will remove everything from the first to the last occurrence of \r
as long as there is the keyword between them.

In my humble opinion it’s not a task for a regex.

How about:


$nl = array("\\r\
", "\\r");

$string = str_replace($nl, "\
", $string);

$in = explode("\
", $string); // use "\
\
" if there are empty lines between paragraphs

foreach($in as $p) {
    if (strstr($p, $keyword) === FALSE) {
        $out[] = $p;
    }
}

$string = implode("\
", $out);

Umm… I have poor regex. That was my just a try only if that just works for OP as well. I just tried that and it seemed that it removed the paragraph/line which has the keyword when I tested because the OP wants to.

Yes off course that is what i also has thought before but I just thought if it could be done in one line with regex then why not try once.

Thank you Pawel for describing the regex above clearly. :slight_smile:

it’s going really well here, both solutions are helping me make a release candidate for the google gettext translation.

I am just trying to figure how to also delete paragraphs with the keyword 'msgstr “”/n"(.*)" ’

i.e. with

#: payment/uc_cybersource/uc_cybersource.module:1030,1059,1059,1059,1059,1030,1059,1059,1059,1030,1059
msgid ""
"The customer's bank account is frozen.<br /><b>Possible action:</b> "
"Review the order or request a different form of payment."
[I][B]msgstr ""
"Le compte bancaire du client est bloqu&#233;.<br /><strong>Action possible "[/B][/I]
": </strong>V&#233;rifier la commande ou demander un autre moyen de "
"paiement."

if i do it on my own it will take me 2 hours!

Wait! it’s ok, PMW’s solution solves that one. It’s erases more of the formatting so i was focusing on the decowsky method for the last hour. :slight_smile: