问题
I want to remove the strings fa and fa-phone of the string: "fa famouse fa-phone".
I tried:
preg_replace('/fa.+$/','', 'fa famouse fa-phone');
But now it removes all strings with fa
.
回答1:
preg_replace('/fa[ \-](phone)?/','','fa famouse fa-phone');
Regex: https://regex101.com/r/bJ3nE7/1
fa
(literal)[ \-]
match a space or a hyphen(phone)?
match the literal 'phone' 0 or 1 times
回答2:
So i want to remove the string fa and all the strings a beginning with fa-
But not other strings that contain fa like famouse for example.
Here is a way to do the job:
echo preg_replace('/fa(?:-[-\w]+|\b)/', '', 'fa famouse fa-phone-small fabrics fantastic'),"\n";
Output:
famouse fabrics fantastic
来源:https://stackoverflow.com/questions/28751418/php-remove-string-fa-fa-phone-famouse