Reputation: 23
Hey, I need to delete a part of a string (I'm not a :
$content = "user=predator1&scode=mau4&athleteid=17007";
$content = "user=predator1&athleteid=17007";
I need to delete the "$code=XXXXX" part (the XXXXX part is variable)
I've tried using this:
$content = preg_replace("/&scode=[^a-z]*/", "", $content);
but doesn't work...
How can I solve this problem?
Thank you!!!
Upvotes: 2
Views: 2238
Reputation:
You should try this line:
$content = preg_replace("/&scode=[a-zA-Z0-9]*/", "", $content);
It says to remove anything which starts with &scode
which is followed by any number of lowercase letter, uppercase letters, or numbers.
In your original code, you were using [^a-z]*
. The ^
in your character list said to not match on any lowercase letters, so it would stop replacing content as soon as it saw a lowercase letter. Basically, your regex would remove only numbers that started a code, but wouldn't remove anything else in the code if it included a lowercase character.
My regex above is an inclusive list, saying to remove anything that's on the list, so it will remove any alphanumeric character.
Hope that helps.
Upvotes: 1
Reputation: 18984
This would work without using a regular expression:
$pre = "user=predator1&scode=mau4&athleteid=17007";
$exp = explode('&', $pre);
$content = implode('&', array($exp[0], $exp[2]));
Upvotes: 0
Reputation: 22010
There is probably a simpler/faster method that involves regex but this method is safe (parse_str
ensures the query string is properly parsed).
$content = "user=predator1&scode=mau4&athleteid=17007";
parse_str($content, $vars);
if (isset($vars['code']))
{
unset($vars['code']);
}
$content = http_build_query($vars);
var_dump($content);
Docs for parse_str
: http://www.php.net/manual/en/function.parse-str.php
Upvotes: 4