Jump to content

How to use preg_replace to replace a particular string


jwhite68

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.