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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -