jwhite68 Posted May 13, 2009 Share Posted May 13, 2009 I have a string, which is of this format: action=listingview&listingID=142&cmsrealty=admin The ID number, 142 in the example, is variable though. How do I strip the '&cmsrealty=admin' from the above string? Assumed I could use preg_replace but cant understand how to use it for above scenario. Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/ Share on other sites More sharing options...
GingerRobot Posted May 13, 2009 Share Posted May 13, 2009 Where does the string come from? It might be easier to just not put it in in the first place? In any case, is &cmsrealty=admin always the last part of the string? If so, you could do something like this: <?php $str = "action=listingview&listingID=142&cmsrealty=admin"; $pieces = explode('&',$str); unset($pieces[count($pieces)-1]); $str = implode('&',$pieces); ?> Or use a combination of strpos and substr Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/#findComment-833086 Share on other sites More sharing options...
jwhite68 Posted May 13, 2009 Author Share Posted May 13, 2009 Cant assume its the last part of the string. Its part of some processing that renames URL's thats part of the system I am using. I need to make sure that when I find this particular string, the replacement is only applied to this string and not others. In your example, $str is actually the search string. This appears in a larger string called $page. When I find $str, I need to strip out the &cmsrealty=admin. Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/#findComment-833129 Share on other sites More sharing options...
GingerRobot Posted May 13, 2009 Share Posted May 13, 2009 Try this then: <?php $str = "action=listingview&listingID=142&cmsrealty=admin"; parse_str($str,$pieces); if(array_key_exists('cmsrealty',$pieces){ unset($pieces['cmsrealty']); } $str = implode('&',$pieces); ?> Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/#findComment-833164 Share on other sites More sharing options...
jwhite68 Posted May 13, 2009 Author Share Posted May 13, 2009 Doesnt the above still assume that the ID=142 is fixed though? Since this is variable, I need to just assume that what comes after ID= is a number. Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/#findComment-833182 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 Does it really matter what comes after ID=? It's not like you're needing that number. You can replace the number with a variable if you want. Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/#findComment-833214 Share on other sites More sharing options...
nrg_alpha Posted May 13, 2009 Share Posted May 13, 2009 Along the lines of what GingerRobot was suggesting with strpos and substr (but in this case, I chose str_replace), perhaps something like: $str = "action=listingview&listingID=142&cmsrealty=admin"; if(strpos($str, '&cmsrealty=admin') !== false){ $str = str_replace('&cmsrealty=admin', '', $str); } ? Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/#findComment-833239 Share on other sites More sharing options...
GingerRobot Posted May 13, 2009 Share Posted May 13, 2009 Doesnt the above still assume that the ID=142 is fixed though? No. The parse_str() function breaks up a string by the & character, with each piece going into $pieces. The code then checks to see if any of the keys of that array are the thing you're looking to remove. If it is, it deletes it. Finally, we glue all the pieces back together with the & character. The only problem with nrg's solution is that, should the argument be in the middle of the string, we'll lose an ampersand. If you decide to not replace the ampersand, then we'll also have to check to see if we have a rogue one on the end of our string. Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/#findComment-833269 Share on other sites More sharing options...
jwhite68 Posted May 13, 2009 Author Share Posted May 13, 2009 When I ran the script, it removed the action= though... <?php $str = "action=listingview&listingID=142&cmsrealty=admin"; parse_str($str,$pieces); if(array_key_exists('cmsrealty',$pieces){ unset($pieces['cmsrealty']); } $str = implode('&',$pieces); ?> The actual 'problem' I probably havent explained clearly enough. I first have to locate the occurence of a string like $str in a larger string. Where the ID=xxx number part is different. How can I use PHP functions to locate this pattern, and then replace that pattern by removing the &cmsrealty=admin? Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/#findComment-833311 Share on other sites More sharing options...
nrg_alpha Posted May 13, 2009 Share Posted May 13, 2009 The only problem with nrg's solution is that, should the argument be in the middle of the string, we'll lose an ampersand. If you decide to not replace the ampersand, then we'll also have to check to see if we have a rogue one on the end of our string. But isn't every field-value pairs delimited by the ampsersand? Would it not be wise to take out the ampersand with the targeted field and value? (in other words, if there is more to the query string, it would start at the next ampersand and field/value...) I went with what I did just for that purpose (in case there is more in the query string).. so for example: $str = "action=listingview&listingID=142&cmsrealty=admin&val=offset"; if(strpos($str, '&cmsrealty=admin') !== false){ $str = str_replace('&cmsrealty=admin', '', $str); } Would output: action=listingview&listingID=142&val=offset Maybe I'm missing something here? :-\ Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/#findComment-833322 Share on other sites More sharing options...
jwhite68 Posted May 13, 2009 Author Share Posted May 13, 2009 Yes, its possible that the action= could be a different action. eg. action=signup (and a bunch of others) for which I dont want to remove the cmsrealty=admin. So, its specifically on the occurence of cmsrealty=admin, for action=listingview, when a listingID is provided. Hope that makes it clearer. Thats why I cant get my head around it! Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/#findComment-833336 Share on other sites More sharing options...
GingerRobot Posted May 13, 2009 Share Posted May 13, 2009 Maybe I'm missing something here? :-\ Naa. It's me that's missing something here - half my brain cells apparently. I'm not really quite sure what i was thinking when i posted that :s Sorry 'bout that. Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/#findComment-833356 Share on other sites More sharing options...
nrg_alpha Posted May 13, 2009 Share Posted May 13, 2009 So, its specifically on the occurence of cmsrealty=admin, for action=listingview, when a listingID is provided. Hope that makes it clearer. So would something like this be along the lines of what you are looking for? $str = "some text, more text; action=listingview&listingID=142&cmsrealty=admin and even some more text as well! action=signup&listingID=341&cmsrealty=admin"; if(preg_match_all('#(action=listingview[^\s]*listingID=\d+[^\s]*)&cmsrealty=admin([^\s]*)#', $str, $matches)){ $count = count($matches[0]); for ($a = 0 ; $a < $count ; $a++) { $str = str_replace($matches[0][$a], $matches[1][$a].$matches[2][$a], $str); } } The [^\s]* will cause some backtracking, but will be more accurate as a result (instead of using .*? or .+?). Maybe I'm missing something here? :-\ Naa. It's me that's missing something here - half my brain cells apparently. I'm not really quite sure what i was thinking when i posted that :s Sorry 'bout that. No worries.. I'm like that from time to time too I just wasn't sure if I was miscalculating something nor not. Link to comment https://forums.phpfreaks.com/topic/157942-how-to-use-preg_replace-to-replace-a-particular-string/#findComment-833363 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.