Reputation: 764
I would like to use RegEx to replace all text that comes after the percent sign in a string. Let's say I have the following string:
/path/to/webpage/%foo
I would like to use RegEx to replace %foo
(this is variable and doesn't necessarily contain foo at all times) with $foo
.
So far I have come up with the following expression, except this doesn't seem to work.
(%([a-z]+))\w
Any suggestions?
Thank you!
Upvotes: 1
Views: 384
Reputation: 2765
Try an easier pattern, no (
subpattern matching needed here, just %.*$
for 'match all characters starting from '%' to the end of line':
<?php
$name = "/path/to/webpage/%foo";
$pattern = '/%.*$/';
$bar = 'bar';
$new_name = preg_replace($pattern, $bar, $name);
echo "$new_name\n";
result:
/path/to/webpage/bar
Upvotes: 1