Given the given String below, how do I define this division?

Asked

Viewed 76 times

1

I have the following string:

"User Data: JOAQUIM DE OIVEIRA, NASCIDO IN 2010, GRADUATED IN: LAW, HOBBIE: FOOTBALL"

How do I give one explode in that string only by validating the first sign of separation and dividing it into two ? Example:

pedaço1="Dados do Usuário";
pedaço2="JOAQUIM DE OIVEIRA, NASCIDO EM 2010, FORMADO EM: DIREITO, HOBBIE: FUTEBOL";

2 answers

3

Use the function explode. View documentation here.

$string = "Dados do Usuário: JOAQUIM DE OIVEIRA, NASCIDO EM 2010, FORMADO EM: DIREITO, HOBBIE: FUTEBOL";
$array = explode(":", $string, 2);

// $array[0] = "Dados do Usuário"
// $array[1] = "..."

2

You can use the preg_match which is similar to explode only it uses a regular expression.

$string = "Dados do Usuário: JOAQUIM DE OIVEIRA, NASCIDO EM 2010, FORMADO EM: DIREITO, HOBBIE: FUTEBOL";
preg_match("/^([^:]*:\s)(.*)/", $string, $pedacos);
echo $pedacos[2]; // "JOAQUIM DE OIVEIRA, NASCIDO EM 2010, FORMADO EM: DIREITO, HOBBIE: FUTEBOL"

Example: http://ideone.com/rZDzvR

In this example you will have an array of 3 elements. The first is the complete string, the second has only "User Data: " and the third ($pedacos[2]) the rest.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.