Hi,
I am very new to expressions, and i am trying to do the following, if its at all possible.
If i have the following string:
<col caption = 'Contact Name';columns = contacts.salutation, contacts.first_name,
I would like to remove the spaces and ' marks, but leave the spaces if they are contain between ' (if this symbol cause a problem then i am willing to chance it to something else)
So it looks like;
<colcaption=Contact Name;columns=contacts.salutation,contacts.first_name,
Many thanks
Removing spaces, but not if contained within a single quote
Moderator: General Moderators
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Removing spaces, but not if contained within a single quote
Try this:
Code: Select all
echo preg_replace(
"/\s(?=([^']*'[^']*')*[^']*$)|'/i",
'',
"<col caption = 'Contact Name';columns = contacts.salutation, contacts.first_name, "
);Re: Removing spaces, but not if contained within a single quote
many thanks for the reply, works great, apart from not removing the ' - but leaving in the space between them.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Removing spaces, but not if contained within a single quote
You're welcome.m8kwr wrote:many thanks for the reply, works great, apart from not removing the ' - but leaving in the space between them.
But it also removes the single quotes from it. I tested the regex with:
Re: Removing spaces, but not if contained within a single quote
Hi,
I used http://regexlib.com/RETester.aspx, and i have regexbuddy - but it doesn't remove the ' in there....
Am i doing something wrong. Sorry i am very new to regex's
I used http://regexlib.com/RETester.aspx, and i have regexbuddy - but it doesn't remove the ' in there....
Am i doing something wrong. Sorry i am very new to regex's
Re: Removing spaces, but not if contained within a single quote
sorry it was me, i forgot to remove the /i, and it is all working perfectly now, many thanks again.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Removing spaces, but not if contained within a single quote
No problem, and you're welcome.m8kwr wrote:sorry it was me, i forgot to remove the /i, and it is all working perfectly now, many thanks again.