Skip to content
PHP Logo

How can you extract only two characters after a comma in a string using PHP?

To extract only two characters after a comma in a string using PHP, you can use the explode() function to split the string into an array based on the comma, and then access the second element of the array to get the characters you need. You can then use substr() to extract only the first two characters from that element.

php
$string = "Hello,World";
$parts = explode(",", $string);
$characters = substr($parts[1], 0, 2);
echo $characters; // Output: Wo