I am trying to remove from a string all characters that do not match to a list of words.
my list of words could be:
- person
- animal
a string can look like this:
- 123-ea-person.jpg
- 456456-on-Person.jpg
- a-animal-dog.png
my result should look like this:
- person
- person
- animal
my approach:
preg_replace('/(person|animal)/i', '', '123-ea-person.jpg')
output:
123-ea-.jpg
expected output:
person
how can i reverse the pattern to get the result?
SOLVED
@Juan thx, extension i don't need.
solution 1 (@Juan):
strtolower(preg_replace('/(.*)(person|animal)(.*)/i', '$2', '123-ea-person.jpg'));
solution 2 (@user3783243):
strtolower(preg_replace('/(?:person|animal)(*SKIP)(*FAIL)|./i', '', '123-ea-person.jpg'));
result:
person
CodePudding user response:
strtolower
with PCRE verbs could achieve your goal:
strtolower(preg_replace('/(?:person|animal)(*SKIP)(*FAIL)|./i', '', '123-ea-person.jpg 456456-on-Person.jpg a-animal-dog.png'));
with this approach a person
or animal
match would be skipped, all other matches would be replaced with nothing.