Skip to content
PHP Logo

How can PHP be used to separate and extract specific data from a string, such as splitting a full name into separate columns for first and last name?

To separate and extract specific data from a string in PHP, you can use functions like explode() or preg_split() to split the string based on a delimiter or pattern. For example, to split a full name into separate columns for first and last name, you can use explode() to split the string by a space and then assign the resulting parts to variables for first and last name.

php
$full_name = "John Doe";
$name_parts = explode(" ", $full_name);
$first_name = $name_parts[0];
$last_name = $name_parts[1];

echo "First Name: " . $first_name . "<br>";
echo "Last Name: " . $last_name;