php REGEX help replace text inside of specific symbols -
i have string 1 being
[my_name] , being <my_name>
i need use regex search text within [ ] , < > brackets , replace bob
i provide sample code don't know begin. appreciated
so far iv tried this
$regex = [\^[*\]]
thinking inside [] tags
i imagine following should work:
preg_replace('/([\[<])[^\]>]+([\]>])/', "$1bob$2", $str);
explanation of regex:
([\[<]) -> first capturing group. here describe starting characters using character class contains [ , < (the [ escaped \[) [^\]>]+ -> stuff comes between [ , ] or < , >. character class says want character other ] or >. ] escaped \]. ([\]>]) -> second capturing group. we describe ending characters using character class. similar first capturing group.
the replacement pattern uses backreferences refer capturing groups. $1
stands first capturing-group can contain either [
or <
. second capturing-group represented $2
, can contain either ]
or >
.
Comments
Post a Comment