Skip to content
Advertisement

PHP preg_replace to replace entire string based on some text matches [closed]

I need to replace the entire portion containing some texts in the string below:

$string='Rich Food Industries is a giant<w:r><w:t xml:space="preserve"> food processing industry situated in Aba,</w:t></w:r><w:r><w:t xml:space="preserve"> Abia state with branches in over </w:t></w:r><w:t xml:space="preserve">10 states across Nigeria. The company was established in</w:t>';

$text1 = 'Rich'; $text2 = 'State'; $text3 = 'in'; $replacement = 'new content goes here'

So far I have

    $word =  preg_replace('~(.*)('.preg_quote($text1).'{1,}) (?:(w+|W+){1,}) ('.preg_quote($text2).'{1,}) (?:(w+|W+){1,}) ('.preg_quote($text3).'{1,})~su', $replacement, $string);   

Note that the first match ($text1) is a word, which can be followed by another word or a ‘<w:>|<w:/>|</w:*>’ tags. Then another word ($text2) which can be followed by a word or sets of tags as before, then the last word ($text3). The entire portion of $string show be replaced with $replacement.

Any help will be appreciated.

Advertisement

Answer

Try '~' . preg_quote($text1) . '.*?' . preg_quote($text2) . '.*?' . preg_quote($text3) . '~' with non-greedy matching.

Of course, any regular expression will break the XML, because you shouldn’t really parse or process XML with regular expressions.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement