Re: strip out repeated ocurrence of a string

From: Date: Tue, 26 Jan 2010 12:56:45 +0000
Subject: Re: strip out repeated ocurrence of a string
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-301468@lists.php.net to get a copy of this message
On 1/25/2010 10:48 PM, Camilo Sperberg wrote:
Hello list :) I have this problem, a certain string can contain the following information: $string = ' hi{value1;value2} bye{value1;value3} hi{value1;value4} hi{value1;value2} bye{value1;value2} '; What I want is to be able to get this result: $string = ' hi{value1;value2} bye{value1;value3} hi{value1;value4} bye{value1;value2} '; (the order of appearance doesn't matter) Is it even possible to do this with regular expressions? Or should I first look if there is some kind of match and then apply an str_replace($match,'',$string) and add the $match again? Greetings !
Assuming the duplicate segments are identical..... I'd use explode() and convert the string to an array. Use "}" for the delimiter. Then use array_unique() And then use implode() to restore the string.

« previous php.general (#301468) next »