user2203747
user2203747

Reputation: 1

Delete string between two commas

"This is a sentence, which is, weirdly seperated, by commas."

How can I delete the sentence between the 2nd comma and the third to get this sentence in PHP:

"This is a sentence, which is .... by commas."

Upvotes: 0

Views: 121

Answers (1)

CR47
CR47

Reputation: 923

$sentence= "This is a sentence, which is, weirdly separated, by commas.";
$pieces = explode(",", $sentence);
echo $pieces[0] .', '. $pieces[1].', ...';

The explode function allows you to make an array of the string and separate each entry by what you put in quotes, in this case, a comma.

The last line concatenates the pieces of the array you want, leaving out the third section aka pieces[2].

Upvotes: 2

Related Questions