简体   繁体   中英

How to replace value in string using regular expression in php?

Here my string ($commit$ + $Bug$)/$HR$*($Leader$^$IT$) ... I want to replace all $variable$ replace with 1...

like (1 + 1)/1*(1^1) ...

is there possible to replace with value 1 ??? how??

Don't care which variable in between $ _ _$...

Please Help me...

$result = preg_replace(
    '/\$ # Match $
    \w+  # Match one or more alphanumeric characters
    \$   # Match $/x', 
    '1', $subject);

This assumes that only the characters [A-Za-z0-9_] are legal between $ and $ .

Try this one

\$(.*?)*\$  Or (\$\w*\$)+
preg_replace('/\$(.*?)*\$/i', '1', '($commit$ + $Bug$)/$HR$*($Leader$^$IT$)');

1] Click Here

2] Better one

Check above links for answer

$str = '($commit$ + $Bug$)/$HR$*($Leader$^$IT$)';
$str = preg_replace('/\$(.*?)\$/', '1', $str);
echo $str;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM