gamefreak13 Posted May 22, 2008 Share Posted May 22, 2008 I know how to do it, but I am not happy with my method because it may not work every time. <?php $input = 'http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid=00000&MyToken=f6e43fc-c41a-4ebb-81bd-68f091107868'; echo substr_replace($input,"",-45); ?> This obviously does what I want it to do. It removes the "&MyToken....." part. However, if there were to ever be anything after it, then it would strip incorrectly. This also works. Preg_replace.. but the problem with this is that it strips everything after "&MyToken...". I think this is the proper code but I need the proper regex. <?php $str = 'http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid=00000&MyToken=f6e43fc-c41a-4ebb-81bd-68f091107868&foo=bar'; $reg = '%&MyToken=(.+)%'; $str = preg_replace($reg,"",$str); echo $str; ?> My goal is to remove only the "&MyToken=f6e43fc-c41a-4ebb-81bd-68f091107868" part without risking removing other important content. Link to comment https://forums.phpfreaks.com/topic/106746-replace-portion-of-text-string/ Share on other sites More sharing options...
thebadbad Posted May 22, 2008 Share Posted May 22, 2008 This should do the trick (using a positive lookahead): <?php $str = 'http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid=00000&MyToken=f6e43fc-c41a-4ebb-81bd-68f091107868&foo=bar'; $reg = '%&MyToken=(.*?)(?=&)%'; $str = preg_replace($reg,"",$str); echo $str; ?> Link to comment https://forums.phpfreaks.com/topic/106746-replace-portion-of-text-string/#findComment-547186 Share on other sites More sharing options...
gamefreak13 Posted May 22, 2008 Author Share Posted May 22, 2008 Works great, but I have to ask.. does the (?-&) part say "stop at the next ampersand" ? Just wondering.. I like to understand the code I work with. $reg = '%&MyToken=(.*?)(?=&)%'; Thanks! EDIT: Uh oh.. the above code only works if there actually is something after the MyToken part (there is 99% chance not going to be). It's good that we have both codes for both occasions, but I need them both to work. Is there a way to say "stop at next ampersand or at end"? Link to comment https://forums.phpfreaks.com/topic/106746-replace-portion-of-text-string/#findComment-547187 Share on other sites More sharing options...
thebadbad Posted May 22, 2008 Share Posted May 22, 2008 Oh sorry, didn't think of that. something(?=&) matches "something" ending with an &, without matching the & part. So it removes the string up until the &foo=bar, while still keeping &foo=bar. This should work: $reg = '%&MyToken=(.*?)((?=&)|$)%'; Swapped (?=&) with ((?=&)|$) meaning it will either do the (?=&) or $ (which means end of string). I.e. if an ampersand is found, it stops there, but if the end of the string is found (= no ampersand), that's alright too Link to comment https://forums.phpfreaks.com/topic/106746-replace-portion-of-text-string/#findComment-547209 Share on other sites More sharing options...
gamefreak13 Posted May 22, 2008 Author Share Posted May 22, 2008 Works great in all variations! Thanks! <?php // Different variations $str1 = 'http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid=00000&MyToken=f6e43fc-c41a-4ebb-81bd-68f091107868&foo=bar'; $str2 = 'http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid=00000&MyToken=f6e43fc-c41a-4ebb-81bd-68f091107868'; $str3 = 'http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid=00000&foo=bar'; $str4 = 'http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid=00000'; echo preg_replace("%&MyToken=(.*?)((?=&)|$)%","",$str1); echo "<hr>"; echo preg_replace("%&MyToken=(.*?)((?=&)|$)%","",$str2); echo "<hr>"; echo preg_replace("%&MyToken=(.*?)((?=&)|$)%","",$str3); echo "<hr>"; echo preg_replace("%&MyToken=(.*?)((?=&)|$)%","",$str4); ?> Link to comment https://forums.phpfreaks.com/topic/106746-replace-portion-of-text-string/#findComment-547225 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.