简体   繁体   中英

REGEX to target all text between two words including the words

I have a WordPress function which I would like a part of the intercept to be removed. Right now the bellow code works 100% only problem is the text isn't static so I need to make a range for it to work on all pots.

This is the code bellow:

    function trim_excerpt($text)
{
    return str_replace('Samurai Swords, Ninja Napkins, Samurai Dishes, Art Plates:,', ' ', $text);
}
add_filter('get_the_excerpt', 'trim_excerpt');

I tried to make a regex to capture all the text Starting from Samurai Swords, until Plates: but I couldn't as I do not know regEx.

(The only standard words will be the Samurai Swords and Art Plates, all the rest can change. That is why I have the word range Samurai Swords -> Plates)

Any help possible?

You could use the below regex to capture all the text starting from Samurai Swords to Art Plates: . [\\S\\s]*? matches any space or non-space characters zero or more times(non-greedy).

(Samurai\s*Swords[\S\s]*?Art\s*Plates:)

Use preg_replace() here instead.

$text = preg_replace('/Samurai\s*Swords.*?Art\s*Plates:/s', '', $text);

This will match everything from "Samurai" to "Plates:" and replace it with an empty value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM