0

I am using a explode and str_replace on the get parameter of the query string URL. My goal is to split the strings by certain characters to get to the value in the string that I want. I am having issues. It should work but doesn't.

Here are two samples of links with the query strings and delimiters I'm using to str_replace.

http://computerhelpwanted.com/jobs/?occupation=analyst&position=data-analyst

as you can see the URL above parameter is position and the value is data-analyst. The delimiter is the dash -.

http://computerhelpwanted.com/jobs/?occupation=analyst&position=business+systems+analyst

and this URL above uses same parameter position and value is business+systems+analyst. The delimiter is the + sign.

The value I am trying to get from the query string is the word analyst. It is the last word after the delimiters.

Here is my code which should do the trick, but doesn't for some reason.

$last_wordPosition = str_replace(array('-', '+')," ", end(explode(" ",$position)));

It works if the delimiter is a + sign, but fails if the delimiter is a - sign.

Anyone know why?

Mike
  • 607
  • 8
  • 30

3 Answers3

1

You have things in the wrong order:

$last_wordPosition = end(explode(" ", str_replace(array('-', '+'), " ", $position)));

You probably want to split it up so as to not get the E_STRICT error when not passing an variable to end:

$words = explode(" ", str_replace(array('-', '+'), " ", $position));
echo end($words);

Or something like:

echo preg_replace('/[^+-]+(\+|-)/', '', $position);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • That's what I was looking for. Works the way it should. I had tried the ordering in different ways and did it like this but apparently I had something off. – Mike Sep 25 '14 at 21:20
0

As @MarkB suggested you should use parse_url and parse_str since it is more appropriate in your case.

From the documentation of parse_url:

This function parses a URL and returns an associative array containing any of the various components of the URL that are present.

From the documentation of parse_str:

Parses str as if it were the query string passed via a URL and sets variables in the current scope.

So here is what you want to do:

$url1 = 'http://computerhelpwanted.com/jobs/?occupation=analyst&position=data-analyst';
$url2 = 'http://computerhelpwanted.com/jobs/?occupation=analyst&position=business+systems+analyst';


function mySplit($str)
{
    if (preg_match('/\-/', $str))
        $strSplited = split('-', $str);
    else
        $strSplited = split(' ', $str);
    return $strSplited;
}

parse_str(parse_url($url1)['query'], $output);
print_r($values = mySplit($output['position']));
parse_str(parse_url($url2)['query'], $output);
print_r($values = mySplit($output['position']));

OUTPUT

Array
(
    [0] => data
    [1] => analyst
)
Array
(
    [0] => business
    [1] => systems
    [2] => analyst
)

You said that you needed the last element of those values. Therefore you could find end and reset useful:

echo end($values);
reset($values);
Adam Sinclair
  • 1,654
  • 12
  • 15
  • nice example here. However, those two urls were just samples. I have many urls with different values in the parameters. For the `occupation` parameter I use `$occupation` which gets from the URL, and then use `$position` for the `position` parameter. – Mike Sep 26 '14 at 00:07
  • @Mike I edited it. This is just an example but I you can easily work with it. You just have to update `$output['position']` by whatever you like. Hope it helped. :) – Adam Sinclair Sep 26 '14 at 00:21
  • I couldn't get your answer to work right, so I did it a different way. Gonna edit my question and post the update below it so you can see. – Mike Sep 27 '14 at 05:26
  • What do you mean it doesn't work ? What output do you get ? I might be able to help. – Adam Sinclair Sep 27 '14 at 20:03
  • I think I was doing it wrong. I've figured it out since then. I appreciate your answer and help. I learned a lot about parse_str and parse_url. – Mike Sep 28 '14 at 03:06
0

Answering my own question to show how I ended up doing this. Seems like way more code than what the accepted answer is, but since I was suggested to use parse_url and parse_str but couldn't get it working right, I did it a different way.

function convertUrlQuery($query) {
    $queryParts = explode('&', $query);

    $params = array();
    foreach ($queryParts as $param) {
        $item = explode('=', $param);
        $params[$item[0]] = $item[1];
    }

    return $params;
}

$arrayQuery = convertUrlQuery($_SERVER['QUERY_STRING']); // Returns - Array ( [occupation] => designer [position] => webmaster+or+website-designer )

$array_valueOccupation = $arrayQuery['occupation']; // Value of occupation parameter

$array_valuePosition = $arrayQuery['position']; // Value of position parameter

$split_valuePosition = explode(" ", str_replace(array('-', '+', ' or '), " ", $array_valuePosition)); // Splits the value of the position parameter into separate words using delimeters (- + or)

then to access different parts of the array

print_r($arrayQuery); // prints the array

echo $array_valueOccupation; // echos the occupation parameters value

echo $array_valuePosition; // echos the position parameters value

print_r($split_valuePosition); // prints the array of the spitted position parameter

foreach ($split_valuePosition as $value) { // foreach outputs all the values in the split_valuePosition array
            echo $value.' ';
}

end($split_valuePosition); // gets the last value in the split_valuePosition array

implode(' ',$split_valuePosition); // puts the split_valuePosition back into a string with only spaces between each word

which outputs the following

arrayQuery = Array
(
    [occupation] => analyst
    [position] => data-analyst
)

array_valueOccupation = analyst

array_valuePosition = data-analyst

split_valuePosition = Array
(
    [0] => data
    [1] => analyst
)

foreach split_valuePosition =

- data
- analyst

end split_valuePosition = analyst

implode split_valuePosition = data analyst
Mike
  • 607
  • 8
  • 30