0

I have a text string that is set in a variable to a value like these:

$str = 'type=showall'

or

$str = 'type=showall&skip=20'
$str = 'type=showall&skip=40'
$str = 'type=showall&skip=60'

and so on.

I need to check to see if there is a "skip" value present in the string, and if so replace it with a new number that is stored in a $newSkip variable and keep the string the same except for the change to the skip value.

For example if the string was:

$str = 'type=showall&skip=20'

and

$newSkip = 40

then I would like this to be returned:

$str = 'type=showall&skip=40'

If there was no skip value:

$str = 'type=showall'

and

$newSkip = 20

then I would like this to be returned:

$str = 'type=showall&skip=20'

I'm fairly new to PHP so still finding my way with the various functions and not sure which one/s are the best ones to use in this scenario when the text/value you're looking for may/may not be in the string.

2
  • 3
    If you are working specifically with query strings, you can use parse_str to turn it into an array, check/replace the key, and http_build_query to turn it back into a string. Commented Sep 17, 2012 at 16:18
  • depending on how much access you have to the code, it might be better to manipulate the skip value before it is assembled to a string Commented Sep 17, 2012 at 16:26

1 Answer 1

3

PHP has a handy function called parse_str() which accepts a string similar to the one you have, and returns an array with key/value pairs. You'll then be able to inspect specific values and make the changes you need.

$str = 'type=showall&skip=20';

// this will parse the string and place the key/value pairs into $arr
parse_str($str,$arr);

// check if specific key exists
if (isset($arr['skip'])){
    //if you need to know if it was there you can do stuff here
}

//set the newSkip value regardless
$arr['skip'] = $newSkip;

echo http_build_query($arr);

The http_build_query function will return the array into the same URI format that you started with. This function also encodes the final string so if you want to see the decoded version, you'll have to send it through urldecode().

References -

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

4 Comments

You can leave out the check isset(...)because if it isn't there he wants to add it. In both cases $arr['skip'] = $newSkip;
@jer - you are correct, but there might be other actions happening that we are un-aware of. The OP might need to know if the replacement was made or not...
true, just pointing out that he wants the skip part added if it isn't there yet.
Thanks Lix and Jeroen - I've managed to get something working pretty quickly thanks to your help in pointing me in the right direction. Much appreciated.

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.