PHP Regex - Append String To Words That Begin with "#' Character
How would I go about using regex to add a static string to all words in another string that begin with the hash character? For example, I have the following two variables:
$stringtoappend = "someuniqueID";
$mystring = ".someClass #someID #someOtherID";
After processing the开发者_JAVA技巧 string, I'd like to create the following string:
".someClass #someIDsomeuniqueID #someOtherIDsomeuniqueID";
Assuming your strings are common CSS names (alphanumeric + dash), use preg_replace()
with the following regular expression:
preg_replace('/(#[\w-]+)/', '$1' . $stringtoappend, $mystring);
精彩评论