6

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,

3 Answers 3

6

If there is exactly one comma between the ~s and an even number of ~s in all, then

preg_replace("/~([^,]*),([^,]*)~/", "~\1\2~", $text) 

should do it.

Sign up to request clarification or add additional context in comments.

Comments

1

It may be easier to do this in a few steps:

  • Split on ~
  • Transform the parts that are "inside" the ~ only
    • Simply replace ',' with ''
  • Join the parts back together with ~

A regex solution

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 

How it works

There are 4 cases:

  • We're at the beginning of the string, "outside"
    • Just match until we find the first ~, so next time we'll be "inside"
    • So, (^[^~]*~)
  • There are no more ~ till the end of the string
    • If there are even number of ~, we'll be "outside"
    • Just match until the end
    • So, ([^~]*$)
  • If it's none of the above, we're "inside"
    • Keep finding the next comma before ~ (so we're still "inside")
      • So, ([^,~]*),
    • If we find ~ instead of a comma, then go out, then go back in on the next ~
      • So, ([^,~]*~[^~]*~)

In all case, we make sure we capture enough to reconstruct the string.

References

Comments

0
$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).

2 Comments

I'm so glad there are regular expressions.
True enough! :) I thought regex might be to specific for what he wanted (eg: works on the example but not on the real thing) :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.