Get `nth` word of URL parameter in PHP

See here for the original answer.

To break the string at a space, you would use the explode function. That separates the parameter and breaks it into an array, so to get the first word you just need to get the 0th index:

$fullname = explode(" ", $_GET["name"])[0];

If space in your URL is encoded as in your example, you just need to replace the space with the encoding:

$fullname = explode("%20", $_GET["name"])[0];

Edit: As pointed out in the comments, the first method should be the way to go because your method of getting the parameter automatically decodes the URL.


Tags

  1. php (Private)
  2. stack-overflow (Private)
  3. answer (Private)