Chintan
Chintan

Reputation: 1204

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...

Upvotes: 0

Views: 84

Answers (3)

softsdev
softsdev

Reputation: 1509

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

Upvotes: 0

keyboardSmasher
keyboardSmasher

Reputation: 2811

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

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336098

$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 $.

Upvotes: 2

Related Questions