user3287726
user3287726

Reputation: 27

How to trim text with PHP?

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?

Upvotes: 1

Views: 134

Answers (2)

Reflection
Reflection

Reputation: 2106

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"

Upvotes: 2

Snehal S
Snehal S

Reputation: 875

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

Upvotes: 0

Related Questions