Forum Moderators: coopster

Message Too Old, No Replies

RegEx and pulling out strings

         

Nutter

2:06 pm on Feb 6, 2005 (gmt 0)

10+ Year Member



I have a string like <!--pagename=Whatever it is--> inside a larger string. What is the easiest was to pull out 'Whatever it is'? I assume some sort of regex command would work, but I'm not great at it. I suppose I could just get the whole string and strip out the first x characters for the <!--pagename= and the last x characters for -->.

Any thoughts?
- Ryan

coopster

8:12 pm on Feb 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Just like you said, there are a number of solutions. A regex would work:
$pattern = '/<!--[^=]+=(.*)-->/is'; 
$matched = preg_replace($pattern, "$1", $string);
It says, find one or more of anything that is not an equal sign after the '<!--', followed by an equal sign, followed by zero or more of anything before the '-->'.