I currently have the string:
"Blah, blah, blah,~Part One, Part Two~,blah blah" I need to remove the comma between the ~ character so it reads.
"Blah, blah, blah,~Part One Part Two~,blah blah" Can anyone help me out please?
Many thanks,
It may be easier to do this in a few steps:
~~ only ',' with ''~That said, it is possible to do this in regex, assuming an even number of ~:
<?php echo preg_replace( '/(^[^~]*~)|([^~]*$)|([^,~]*),|([^,~]*~[^~]*~)/', '$1$2$3$4', 'a,b,c,~d,e,f~,g,h,i,~j,k,l,~m,n,o~,q,r,~s,t,u' ); ?> The above prints (as seen on codepad.org):
a,b,c,~def~,g,h,i,~jkl~m,n,o~qr~s,t,u There are 4 cases:
~, so next time we'll be "inside"(^[^~]*~)~ till the end of the string ~, we'll be "outside"([^~]*$)~ (so we're still "inside") ([^,~]*),~ instead of a comma, then go out, then go back in on the next ~ ([^,~]*~[^~]*~)In all case, we make sure we capture enough to reconstruct the string.
$string = "Blah, blah, blah,~Part One, Part Two~,blah blah"; $pos1 = strpos($string, "~"); $substring = substr($string, $strpos, strlen($string)); $pos2 = strpos($string, "~"); $final = substr($substring, $pos1, $pos2); $replaced = str_replace(",", "", $final); $newString = str_replace($final, $replaced, $string); echo $newString; It does the job, but I wrote it right here and might have problems (at least performance problems).