开发者

PHP Regex - Append String To Words That Begin with "#' Character

开发者 https://www.devze.com 2023-04-04 20:57 出处:网络
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:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消