The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
PHP - Remove single instances
Discuss Remove single instances in the Regex Programming forum on Dev Shed. Remove single instances Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 13th, 2012, 05:19 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
|
PHP - Remove single instances
Hello. I'm sure this is straight-forward but.. I want to remove single instances of the @ symbol, but if there are two consecutive such characters I want to reduce this to one.
So fun@ction should become function and someone@@mail.com should drop to someone@mail.com. Current code:
PHP Code:
$line = preg_replace('/\@{2,}/', '@', $line);
Andy.
|

September 13th, 2012, 05:26 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
And... you don't know if that's the best way to do it?
If there's only two instances then just str_replace() it. Otherwise, if you want to also replace more than two instances, you've got the right idea.
|

September 13th, 2012, 05:36 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
|
My current code replaces two (or more) instances with one, but it doesn't remove the single instances, so I'm still left with fun@ction.
|

September 13th, 2012, 05:46 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
|
Well, this works
$line = preg_replace('/\@{2,}/', '', $line);
to remove two or more instances and leave single instances. So I can use fun@@ction and someone@mail.com - but I'd rather it were the other way round..? Andy.
|

September 13th, 2012, 06:11 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
Never mind:
Code:
$line = preg_replace('/((\@)(\@)?)/', '$3', $line);
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|