I have no experience using regular expressions in PHP, so I usually write some convoluted function using a series of str_replace(), substr(), strpos(), strstr() etc (you get the idea).
This time I want to do this correctly, I know I need to use a regex for this, but am confused as to what to use (ereg or preg), and how exactly the syntax should be.
NOTE: I am NOT parsing HTML, or XML, and sometimes I will be using delimiters other than (for example, | or ~ or [tag] or ::). I am looking for a generic way to do a wildcard replace in between two known delimiters using regex, I am not building an HTML or XML parser.
What I need is a regex that replaces this:
<sometag>everything in here</sometag>
with this:
<sometag>new contents</sometag>
I have read the documentation online for a bit, but I am confused, and am hoping one of you regex experts can pop in a simple solution. I suspect I will pass the values to a function, something like this:
$new_text = swapText ( "<sometag>", $the_new_text_to_go_into_the_dag );
function swapText ( $in_tag_with_brackets_to_update, $in_new_text ) {
// define tags
$starting_tag = $in_tag_with_brackets_to_update;
$ending_tag = str_replace( "<", "</", $in_tag_with_brackets_to_update) );
// not sure if this is the proper regex match string or not
// and/or if any escaping needs to be done on the tags
$find_string = "{$starting_tag}.*{$ending_tag}";
$replace_with_string = "{$starting_tag}{$in_new_text}{$ending_tag}";
// after some regex, this function should return new version of <tag>data</tag>
}
Thanks.