Jump to content

Removing double character


Recommended Posts

Hi all


I have the following value

$var = 'hello|goodbye|morning|evening';

and i'm doing some str_replace to remove certain words, for example

    $var = str_replace('goodbye', '', $var);

which is all cool, but it leaves me with

$var = 'hello||morning|evening';

how can I tell PHP to strip multiple instances of ||, so it would look like

$var = 'hello|morning|evening';

sometimes im left with a value such as

$var = '|||evening';

so I need to strip multiple | symbols into just 1



Link to comment
Share on other sites

Use the PCRE (preg_*) functions to do this? You can easily do this with something very simple as $regex = '/(hello|goodbye|morning|evening)(?:\|)/'; which matches either "hello", "goodbye", "morning" or "evening", followed by "|" (| is a control character in Regular Expressions so we have to escape it).

Link to comment
Share on other sites

You are going to end up with the delimiter left at the beginning or at the end as well as doubled-up in the middle. The most effective way I see to do this is to explode the list (to an array), remove the entries (from the array), and implode the array back into a list ...




$var = 'hello|goodbye|morning|evening';
$list = explode('|', $var);

if ( ($ind = array_search('goodbye', $list)) !== false) unset($list[$ind]);

if ( ($ind = array_search('morning', $list)) !== false) unset($list[$ind]);

$var = implode('|', $list);
Altough a function might be cleaner, it incurs the explode/implode overhead for each removal:

function removeFrom($psList, $psEntry) {
  $list = explode('|', $psList);
  if ( ($ind = array_search($psEntry, $list)) !== false) unset($list[$ind]);
  return implode('|', $list);

$var = 'hello|goodbye|morning|evening';

$var = removeFrom($var, 'goodbye');
$var = removeFrom($var, 'morning');
Link to comment
Share on other sites



I was thinking the same thing. But, you could do the same thign with using str_replace() first to make it even easier by not having to loop over the array and test each value. After using str_replace, convert to an array and then use array_filter() to remove the empty values. Then implode() back.



$var = 'hello|goodbye|morning|evening';
$var = str_replace('goodbye', '', $var); //Remove value
$list = explode('|', $var); //Explode into array
$list = array_filter($list); //Remove empty values in array
$var = implode('|', $list); //Implode back into a string
Edited by Psycho
Link to comment
Share on other sites

I have come up against this before and found the best solution to be $var = implode('|',explode('||',$var))


Which would fail if you had three or more '|' in succession and would not resolve the problem where a '|' is at the beginning or the end of the string.

Link to comment
Share on other sites

Just because I was curious, I went ahead and created a regular expression that can take care of this in a single line:


$text = preg_replace("#(^\|+)|(?<=\|)\|+|(\|+$)#", '', $text);


Although, it's not really worth considering since it performs so fast, I tested this against my previous solution and found that the one which was faster was dependent upon the value to be replaced. The more occurrences of the delimiter used to split the string into an array caused the explode/implode method to be slower compared to regex.


I also tried another method. trim() the delimiter, then create a recursive loop to remove repetitive instances of the delimiter. This was again faster/slower than the other methods based upon the number of delimiters (we are talking milliseconds here). This really has no real impact, I like to do these things to understand the more about the technology and thought I'd share.



        $text = trim($text, '|');
        while(strpos($text, '||') !== false)
            $text = str_replace('||', '|', $text);
Link to comment
Share on other sites

  • 2 weeks later...

Which would fail if you had three or more '|' in succession and would not resolve the problem where a '|' is at the beginning or the end of the string.


Good point Psycho! I've used this to replace markers in a a string but you're right, it would fail in those circumstances.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.