Jump to content

Recommended Posts

personally I would do it like this:

 

preg_match("~?>(.*)~s", $input, $out);
echo $out[1];

 

- I would remove the "return;" to make it more generic. 

- I'd also use * instead of + as the quantifier in the event that there isn't anything after "?>".

- I would also remove the ? to make it greedy; since you're aiming to match everything after, it would be more efficient to make the quantifier greedy. 

- I would remove the $ and use s modifier instead.  The dot by default matches up to the end of line, so $ is not necessary.  But the context of his subject implies he's looking for more than a 1-line match, so I added the 's' modifier in there to make the dot also match newline chars

 

 

I knew somebody would come up with a better version, i just like testing out my knowledge then comparing what somebody more versed comes up with. :)

 

The code which you and CW provided is very useful. (not just in this case). In future cases i can refer and remember how you did it and learn. I started off not knowing anything about php until i joined these forums (view my post history). Ever since my code has become more tidyer with the thanks of the PHPfreaks members.  :D

personally I would do it like this:

 

preg_match("~?>(.*)~s", $input, $out);
echo $out[1];

 

I think that the forum either ate a blackslash or you missed it out accidentally. For the sake of people visiting this thread to copy/paste, your regex will cause an error ("nothing to repeat at offset 0") since the question mark is still trying to behave as a quantifier even though there is nothing before it.

 

Personally, I would do it ever-so-slightly differently with either:

 

preg_match("/\?>\K.*/s", $input, $match);
echo $match[0];

 

or

 

preg_match("/(?<=\?>).*/s", $input, $match);
echo $match[0];

 

Because:


  • [o]The regex engine won't need to remember the entire match and a capturing group containing the same value.
    [o]You get regex-fu credits if you use
\K and/or a lookbehind assertion. (Bonus power-up for the \K)
[o]Neither cause a (regex) compilation failure.
[o]It's always nice to see alternative approaches to the same situation.

Hmm.. I wonder if this is the infamous posting system rearing its ugly head (again) by devouring backslashes and the like.. I find that if you go straight to posting your post (without previewing), you're good to go.. but if you preview (when posting in Chrome / FF) kiss stuff like \ goodbye!

 

I still don't understand what it is that is wrecking posts like that.. I have none of those issues in other website forums.. only this one.. forces my hand to use Opera instead for posts here.. Grrrrr...  :facewall:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.