2

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!!!

6 Answers 6

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

Thank you, but if the last character of the substring is an alphanumeric one like $content = "user=predator1&scode=mau4" (and not the & of another parameter), how can I setup the regular expression to manage this option?
@Gabriele: I'm not sure what you're asking. The regex in my answer does match that string.
@Gabriele: Do you mean "what if scode is the first variable, and therefore doesn't have a leading &? You can do that with pretty easily. If that's what you mean, let me know and I'll update my answer.
Thank you, but at the end I've used the John Himmelman version. Is working perfectly without any problem!
0

Shouldn't "/&scode=[^a-z]*/" be more like "/&scode=[a-zA-Z0-9]*/"?

Comments

0
preg_replace("/code=[a-zA-Z0-9]*/", "code=", $content); 

Comments

0
$content = preg_replace("|&scode=[a-zA-Z0-9]+&|", '&', $content);

Comments

0

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]));

1 Comment

Only if there are only three parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.