#1
  1. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165

    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.
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    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.
  4. #3
  5. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    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.
  6. #4
  7. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    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.
  8. #5
  9. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Never mind:

    Code:
    $line = preg_replace('/((\@)(\@)?)/', '$3', $line);

IMN logo majestic logo threadwatch logo seochat tools logo