Created
September 8, 2012 14:05
-
-
Save jerryharrison/3675208 to your computer and use it in GitHub Desktop.
Removing text after a certain character - PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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