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,
On the Home tab, click the Dialog Box Launcher next to Number. On the Number tab, in the Category list, click Number. To display or hide the thousands separator, select or clear the Use 1000 Separator (,) check box.
You can use right to check and see if the last character is a comma, and a case statment will either remove the last comma with a substring, or display the field if it does not end in comma.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With