dweb Posted June 21, 2013 Share Posted June 21, 2013 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 thanks Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 21, 2013 Share Posted June 21, 2013 do another str_replace on those chars? Quote Link to comment Share on other sites More sharing options...
Irate Posted June 21, 2013 Share Posted June 21, 2013 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). Quote Link to comment Share on other sites More sharing options...
kicken Posted June 21, 2013 Share Posted June 21, 2013 Simple, $var=str_replace('||', '|', $var);Replace two || with a single |. You could do it in a loop if necessary. Or use a regex to replace any number of | with a single |. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 21, 2013 Share Posted June 21, 2013 Upon further review, your original str_replace could include the | character along with the item to be removed: str_replace("|goodbye",""); str_replace("goodbye|",""); Quote Link to comment Share on other sites More sharing options...
DavidAM Posted June 21, 2013 Share Posted June 21, 2013 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 ... Untested: $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'); Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 21, 2013 Share Posted June 21, 2013 Event better idea! Very elegant. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 21, 2013 Share Posted June 21, 2013 (edited) @DavidAM, 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 June 21, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
melles Posted June 26, 2013 Share Posted June 26, 2013 I have come up against this before and found the best solution to be $var = implode('|',explode('||',$var)) Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 26, 2013 Share Posted June 26, 2013 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 26, 2013 Share Posted June 26, 2013 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); } Quote Link to comment Share on other sites More sharing options...
melles Posted July 4, 2013 Share Posted July 4, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.