newbtophp Posted October 6, 2009 Share Posted October 6, 2009 How would i retrieve all the code afer: after: return;?> For example: <?php echo "PHPFreaks is awesome!"; return;?> 37543858935+==== Heyass I'd get: 37543858935+==== Heyass Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/ Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 If you want all the code, something like this should surfice... preg_match("/return;\?> (.+?)$/", $input, $out); echo $out[1]; Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931727 Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931738 Share on other sites More sharing options...
newbtophp Posted October 6, 2009 Author Share Posted October 6, 2009 Great, thanks Cags and Crayon Violent! Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931764 Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931768 Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 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. That's how I learn, as well. Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931773 Share on other sites More sharing options...
newbtophp Posted October 6, 2009 Author Share Posted October 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931779 Share on other sites More sharing options...
nrg_alpha Posted October 6, 2009 Share Posted October 6, 2009 Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931782 Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 to be fair though, regex is a big scary gray area to most programmers, even seasoned veterans. Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931805 Share on other sites More sharing options...
salathe Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931817 Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 whoops, I did forget to escape the ?. Good catch! I never thought of using \K before, brilliant! Is the lookbehind assertion really more optimal, or are you just being fancy? Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931826 Share on other sites More sharing options...
salathe Posted October 6, 2009 Share Posted October 6, 2009 I don't know about performance (I doubt there would be a huge difference between the two anyway) but the lookaround assertions may be more familiar to people than the magical, mystery \K escape sequence. Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931835 Share on other sites More sharing options...
newbtophp Posted October 6, 2009 Author Share Posted October 6, 2009 Yep i saw the backslash error and then looked at Cags regex and he had backslashes before the php end tag so i added it to yours and it worked perfect preg_match("~\?>(.*)~s" Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931837 Share on other sites More sharing options...
nrg_alpha Posted October 6, 2009 Share Posted October 6, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931847 Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 nah, in this case, I forgot to escape it. I used my superhero ability to go back in time and watched myself (not) do it. Quote Link to comment https://forums.phpfreaks.com/topic/176725-solved-get-all-code-after-php-tag/#findComment-931856 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.