Reputation: 511
$value = "ABCC@CmCCCCm@CCmC@CDEF";
$clear = preg_replace('/@{1,}/', "", $value);
I need to remove duplicate @ and get something like:
ABCC@CmCCCCmCCmCCDEF
(I need only first @)
How to do that?
Upvotes: 0
Views: 1096
Reputation: 89567
A regex way:
$clear = preg_replace('~(?>@|\G(?<!^)[^@]*)\K@*~', '', $value);
details:
(?: # open a non capturing group
@ # literal @
| # OR
\G(?<!^) # contiguous to a precedent match, not at the start of the string
[^@]* # all characters except @, zero or more times
)\K # close the group and reset the match from the result
@* # zero or more literal @
Upvotes: 4
Reputation: 3665
Give this a try:
// The original string
$str = 'ABCC@CmCCCCm@CCmC@CDEF';
// Position of the first @ sign
$pos = strpos($str, '@');
// Left side of the first found @ sign
$str_sub_1 = substr($str, 0, $pos + 1);
// Right side of the first found @ sign
$str_sub_2 = substr($str, $pos);
// Replace all @ signs in the right side
$str_sub_2_repl = str_replace('@', '', $str_sub_2);
// Join the left and right sides again
$str_new = $str_sub_1 . $str_sub_2_repl;
echo $str_new;
Upvotes: 3