remove duplicate from string in PHP
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
How to remove duplicate letters in string in PHP
I tend to avoid regex when possible. Here, I'd just split all the letters into one big array and then use array_unique()
to de-duplicate:
$unique = array_unique(str_split(implode('', $elem)));
That gives you an array of the unique characters, one character per array element. If you'd prefer those as a string, just implode the array:
$unique = implode('', array_unique(str_split(implode('', $elem))));
How to remove duplicate words from string using php?
I don't know if php have such function, but you can process it like this: live demo
$raw = 'AAA,BBB,aAA,BbB,AAA,BbB';
$string = implode(',', array_unique(explode(',', $raw)));
What's the best way to remove duplicates from a string in PHP (or any language)?
$arr = explode( " " , $string );
$arr = array_unique( $arr );
$string = implode(" " , $arr);
Remove duplicate from string
array_unique($string); //this is for an array,
$new_var = implode(',',array_unique(explode(',', $string))); //this is for a string
Removing duplicates from strings based on container [] {} ()
$input = '[1] {1} [2] {10} [3] [3] [4] {10} {100} [1] [5]';
$result = array_unique(explode(' ', $input));
Edit: Added two solutions for cases where there are no or multiple spaces.
The first solution preservers whitespaces as they are in the input string:
$result = array_reduce(
str_split($input),
function (array $carry, string $char) {
return match ( $char ) {
'(', '{', '[' => [ $carry[0], $char ],
')', '}', ']' => str_contains($carry[0], $carry[1] . $char)
? $carry
: [ $carry[0] . $carry[1] . $char, '' ],
' ' => [ $carry[0] . ' ', '' ],
default => [ $carry[0], $carry[1] . $char ]
};
},
[ '', '' ]
)[0];
The second one is variation, inserting one space between each item:
$result = array_reduce(
str_split($input),
function (array $carry, string $char) {
return match ( $char ) {
'(', '{', '[' => [ $carry[0], $char ],
')', '}', ']' => str_contains($carry[0], $carry[1] . $char)
? $carry
: [ $carry[0] . ( $carry[0] === '' ? '' : ' ' ) . $carry[1] . $char, '' ],
default => [ $carry[0], $carry[1] . $char ]
};
},
[ '', '' ]
)[0];
Compare duplicate words and remove them from a string in php
That's because array_unique()
reduces duplicates to one value:
Takes an input array and returns a new array without duplicate values.
src
You need to loop the array first (though can imagine a lot of creative array_filter/array_walk stuff possibly):
$string = 'Super this is a test this is a test';
# first explode it
$arr = explode(' ', $string);
# get value count as var
$vals = array_count_values($arr);
foreach ($arr as $key => $word)
{
# if count of word > 1, remove it
if ($vals[$word] > 1) {
unset($arr[$key]);
}
}
# glue whats left together
echo implode(' ', $arr);
fiddle
And as a function for general project-use:
function rm_str_dupes(string $string, string $explodeDelimiter = '', string $implodeDelimiter = '')
{
$arr = explode($explodeDelimiter, $string);
$wordCount = array_count_values($arr);
foreach ($arr as $key => $word)
{
if ($wordCount[$word] > 1) {
unset($arr[$key]);
}
}
return implode($implodeDelimiter, $arr);
}
# example usage
echo rm_str_dupes('Super this is a test this is a test');
php remove duplicates from string, if duplicate length is 4
Update
Based on comments from OP, the required regex is
/(^| )(.{4,}) (.*)\2/
This looks for a group of 4 or more characters preceded by either a space or the start of the line and followed by a space, some number of other characters and then the group repeated again. The regex is replaced by $1$2 $3
which effectively removes the duplicate string. A couple of examples:
$seoproducttitle = 'Apple MacBook Air MacBook Air - 13.3 inch - Intel Core i5-8e - MRE82N/A';
echo preg_replace('/(^| )(.{4,}) (.*)\2/', "$1$2 $3", $seoproducttitle) . "\n";
$seoproducttitle = 'HP Chromebook 11 G5 EE Chromebook - 11.6 inch - Intel® Intel® Celeron® - 4LT18EA#ABH 4LT18EA#ABH';
echo preg_replace('/(^| )(.{4,}) (.*)\2/', "$1$2 $3", $seoproducttitle) . "\n";
Output:
Apple MacBook Air - 13.3 inch - Intel Core i5-8e - MRE82N/A Array
HP Chromebook 11 G5 EE - 11.6 inch - Intel® Celeron® - 4LT18EA#ABH
Updated demo on 3v4l.org
Original Answer
You could use this regex:
\b([^ ]{4,})( |$)(.*)\1
It looks for a group of 4 or more non-blank characters, followed by a space or end-of-string, followed by some number of other characters and then the first group repeated. The regex is replaced by $1$3
which effectively removes the duplicate string. e.g.
$seoproducttitle = 'HP Chromebook 11 G5 EE Chromebook - 11.6 inch - Intel® Intel® Celeron® - 4LT18EA#ABH 4LT18EA#ABH';
echo preg_replace('/\b([^ ]{4,})( |$)(.*)\1/', "$1$3", $seoproducttitle);
Output:
HP Chromebook11 G5 EE - 11.6 inch - Intel® Celeron® - 4LT18EA#ABH
Demo on 3v4l.org
How to remove duplicates from a PHP string separated by X?
The file is read into an array with file. With array_map and preg_replace the spaces and the X are removed from each line. array_unique then removes the duplicate entries.
$array = file('input.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$array = array_map(function($v){return preg_replace('/ +X;/',';',$v);}, $array);
$array = array_unique($array);
The result is an array.
array (
0 => "13:BOOT2700;27",
4 => "13:BOXER1136;11.36",
5 => "13:BOXER1364;13.64",
6 => "13:BOXER1591;15.91",
7 => "13:BOXER909;9.09",
)
If a file is required as a result, the array can be converted into a string with implode and written to a file with file_put_contents.
$str = implode("\r\n",$array);
file_put_contents('input.csv', $str);
Related Topics
Mysql_Num_Rows(): Supplied Argument Is Not a Valid MySQL Result Resource
Do Login Forms Need Tokens Against Csrf Attacks
Can't Install Laravel Installer via Composer
How to Create Optional Arguments in PHP
Using Wildcards in Prepared Statement - MySQLi
PHP Array_Filter with Arguments
Increment Value in MySQL Update Query
Undefined Variable Problem with PHP Function
Calculate the Time When the Sun Is X Degrees Below/Above the Horizon
How to Get Client Ip Address in Laravel 5+
Using JSON_Encode on Objects in PHP (Regardless of Scope)
Single Quotes or Double Quotes for Variable Concatenation
Import File Size Limit in PHPmyadmin
How to Check and Set Max_Allowed_Packet MySQL Variable
Dynamic Class Method Invocation in PHP
Disable Certificate Verification in PHP Soapclient
Differencebetween Find(), Findorfail(), First(), Firstorfail(), Get(), List(), Toarray()
Using PHP/Apache to Restrict Access to Static Files (Html, CSS, Img, etc)