How to get everything after a certain character? Ask Question

How to get everything after a certain character? Ask Question

I've got a string and I'd like to get everything after a certain value. The string always starts off with a set of numbers and then an underscore. I'd like to get the rest of the string after the underscore. So for example if I have the following strings and what I'd like returned:

"123_String" -> "String"
"233718_This_is_a_string" -> "This_is_a_string"
"83_Another Example" -> "Another Example"

How can I go about doing something like this?

ベストアンサー1

The strpos() finds the offset of the underscore, then substr grabs everything from that index plus 1, onwards.

$data = "123_String";
$whatIWant = substr($data, strpos($data, "_") + 1);
echo $whatIWant;

If you also want to check if the underscore character (_) exists in your string before trying to get it, you can use the following:

if (($pos = strpos($data, "_")) !== FALSE) {
    $whatIWant = substr($data, $pos+1);
}

おすすめ記事