Skip to content

Instantly share code, notes, and snippets.

@jerryharrison
Created September 8, 2012 14:05
Show Gist options
  • Save jerryharrison/3675208 to your computer and use it in GitHub Desktop.
Save jerryharrison/3675208 to your computer and use it in GitHub Desktop.
Removing text after a certain character - PHP
$user['User']['name'] = 'Jerry Harrison';
echo $user['User']['name'];
// prints 'Jerry Harrison'
// process with strpos and substr;
// We use strpos to find the position of the character we wish to remove characters after, it returns the first one
// Next we put that number into substr - substr('string', [number of chars to take off the front], [number of chars to count from the start])
// So with that said using zero as the first param and the position of the character we wish to replace as the second we've successfully removed
// the unwanted characters, the last name of the person.
echo substr($user['User']['name'], 0, strpos($user['User']['name'], ' '));
// prints 'Jerry';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment