Home >Backend Development >PHP Tutorial >How to Extract Data After an Underscore in a String?
How to Retrieve Data After a Specific Character in a String?
Given a string that consistently starts with numbers followed by an underscore character, the goal is to retrieve the rest of the string after the underscore. Here's how to accomplish this task:
For instance, with the string "123_String," the strpos() function returns 3 (the position of the underscore). Then, substr() is invoked as follows:
$whatIWant = substr($data, strpos($data, "_") + 1);
This line assigns the value "String" to the $whatIWant variable.
if (($pos = strpos($data, "_")) !== FALSE) { $whatIWant = substr($data, $pos+1); }
In the above example, the code checks if the strpos() function returns a value other than FALSE (indicating the underscore is found). If so, it proceeds to retrieve the data after the underscore.
The above is the detailed content of How to Extract Data After an Underscore in a String?. For more information, please follow other related articles on the PHP Chinese website!