0

I have the following code which gives me exactly what I first needed (uppercase words after the first = in the string $data:

if (($pos = strpos($data, "=")) !== FALSE) { 
    $whatIWant = ucwords(substr($data, $pos+1)); 
}

What I now need to do is exactly the same thing, but ignore anything after (and including) the first & in the same string ($data).

My PHP is weak, please can someone tell me how to update the above to do this? I haven't been able to find the answer here but if I've missed it please tell me.

Sample strings:

destination=Apartment+TITLIS+Resort+Wohnung+721&hotelid=0123454656

destination=Rental+Apartment+Mendi+Eder+-+Saint-Jean&hotelid=01234

destination=Three-Bedroom+Holiday+Home+in+Olofstrom&hotelid=98

4
  • 2
    start with posting your $data Commented Oct 18, 2017 at 19:16
  • I've added some samples as requested - thanks Commented Oct 18, 2017 at 19:19
  • 2
    @chris85 They are URLs, I'm using urldecode($_SERVER['QUERY_STRING']) to get them. I don't know about the exploding yet I'll give that a look Commented Oct 18, 2017 at 19:21
  • @chris85 This is the only PHP I've ever done, there is no reason for me not using $_GET['destination'] other than not knowing about it :) if that will work then it seems straightforward Commented Oct 18, 2017 at 19:29

3 Answers 3

4

If this query string is coming from the URL that is used to access the page (as it seems from your comment that you are using $_SERVER['QUERY_STRING']) then just use the $_GET Superglobal:

echo ucwords($_GET['destination']);

If the query string is coming from somewhere else then there is a tool for that, parse_str:

parse_str($data, $result);

Your first example query string yields:

Array
(
    [destination] => Apartment TITLIS Resort Wohnung 721
    [hotelid] => 0123454656
)

So then just:

echo ucwords($result['destination']);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you - does this also do the upper case function or do I just add ucwords around the $result['destination']?
It does not change case, if you need title case then yes, ucwords.
Thanks one more question, how do I replace the string value with the URL ($data) that I'm currently getting from urldecode?
@nathanhayfield: yes but that will parse the URL and give you the query string which OP already has. Then parse_str is still needed.
2

Your solution here: you can check here: https://3v4l.org/ohgvE

<?php 
$data = "destination=Apartment+TITLIS+Resort+Wohnung+721&hotelid=0123454656";
if (($pos = strpos($data, "=")) !== FALSE) { 
    $whatIWant = ucwords(substr($data, $pos+1)); 
    $whatIWant = explode("&", $whatIWant); // creating array from string
    $whatIWant = $whatIWant[0]; // before & is first array element
    echo "Ans1: \n";
    echo $whatIWant;
    // Output: Apartment+TITLIS+Resort+Wohnung+721
    // if you want your output with & just add & 
    echo "\n\nAns2: \n";

    $whatIWant = $whatIWant."&";

    echo $whatIWant;
    // now Output iss: 
    // Apartment+TITLIS+Resort+Wohnung+721&
}

?>

Comments

1

Using the $_GET superglobal is the proper way to get a GET parameter in PHP.

$_GET['destination']

will have your destination, and it will already be url decoded.

The GET variables are passed through urldecode().

Comments

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.