The shortest code would be:
$str = implode(',',array_unique(explode(',', $str)));
If it is the fastest… I don’t know, it is probably faster then looping explicitly.
Reference: implode
, array_unique
, explode
More Related Contents:
- How do I check if a string contains a specific word?
- Split a comma-delimited string into an array?
- startsWith() and endsWith() functions in PHP
- Why does PHP consider 0 to be equal to a string?
- Replacing accented characters php
- How to remove accents and turn letters into “plain” ASCII characters? [duplicate]
- Convert a comma-delimited string into array of integers?
- How to get everything after a certain character?
- Get first 100 characters from string, respecting full words
- PHP remove special character from string
- Iterate over each line in a string in PHP
- Generate SEO friendly URLs (slugs) [closed]
- PHP: How can I explode a string by commas, but not wheres the commas are within quotes?
- How to get int instead string from form?
- Print newline in PHP in single quotes
- PHP: How to sort the characters in a string?
- Multi-line strings in PHP
- Why does PHP convert a string with the letter E into a number?
- Check if URL has certain string with PHP
- PHP: Split string into array, like explode with no delimiter
- How to open file in PHP that has unicode characters in its name?
- how to extract links and titles from a .html page?
- Remove Trailing Slash From String PHP
- Measure string size in Bytes in php
- Explode a paragraph into sentences in PHP
- Combine two 2d arrays and have duplicate rows removed
- Remove first 4 characters of a string with PHP
- PHP extract link from tag [duplicate]
- How to count the consecutive duplicate values in an array?
- PHP Find all occurrences of a substring in a string