I’m trying to find the best way to do this in PHP :
I have to analyse a string.
Some characters are forbidden (i.e. : comma, semicomma, space, percentage… but it can be any character I want, not only punctuation signs !)
I would like to print a line FOR EACH forbidden character in the string :
$string = "My taylor, is rich%";
After analyse, I want to print :
Character COMMA is forbidden
Character PERCENTAGE is forbidden
The summum could be to have only one line for multiple errors with the same character.
Did some of you experienced such a problem ?
I’ve tried REGEX and STRPOS, but without significant result.
Use preg_match_all.
The output of the above is:
The
preg_match_allwill return all instances of the$forbiddenregex. You can adjust the characters in that regex as you see fit. Thearray_uniquewill eliminate duplicates.Finally, I am just outputting the characters themselves in the output. If you want words like “COMMA”, “PERCENTAGE”, etc…, you will have to create a hash for that.