Question

I have a problem trimming some text with PHP. I had a text that looked something like that:

$teams = "team1-team2"

I needed to trim it so I get the first team and the second one and I did so by using this piece of code:

$team1= substr($teams, 0, strpos($teams, "-"));

$team2= substr($teams, strpos($teams, '-') + 1);

However now I have something like this: team1-team2-team3

How could I trim the string so I get the teams like I did before?

Était-ce utile?

La solution

You can use the explode() function. For example:

$teams = explode("-", "team1-team2-team3");

echo $teams[0]; // prints "team1"
echo $teams[1]; // prints "team2"
echo $teams[2]; // prints "team3"

Autres conseils

You use "explode" function of php with character to explode string

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top