Mr.Happy
Mr.Happy

Reputation: 2647

How to remove duplicate words from the string in PHP

I have following string with repeated words like:

$string = 'This is titleThis is titleThis is Title';

Now I want to remove duplicate words This is titleThis is Title as repeated for two times in my string.

I am looking for this output: This is title.

I have tried with implode(',',array_unique(explode(',', $string))) but not getting luck.

Upvotes: 0

Views: 1273

Answers (1)

Ravi Patel
Ravi Patel

Reputation: 5231

Please use this one code. Explode the string on a space instead of a ,

<?php
$string = 'This is titleThis is titleThis is Title';

echo implode(' ',array_unique(explode(' ', $string)));

Upvotes: 1

Related Questions