Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    Can I use preg_replace() to get rid of any characters that aren't a-z, A-Z or 0-9 in a string?

    ... or is there a PHP function that already does this?

    Thanks ^-^
    tekp :cheeky: tekponline.com

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times


  4. #3
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    But is there a way to say anything that isn't that? At the moment the expression matches all letters and numbers, can I do one that matches everything but letters and numbers?
    tekp :cheeky: tekponline.com

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times
    There are several ways, but here's something ...

    <?php
    $string = "abcDef123.iuj*234_ki:sekKKK&HI^%kmn m,@#3K";
    $answer1 = preg_replace('/[^a-zA-Z0-9]/','',$string);
    $answer2 = preg_replace('/[a-zA-Z0-9]/','',$string);
    echo "Original String: $string <br>\n";
    echo "Only letters/numbers: $answer1 <br>\n";
    echo "Only Everything else: $answer2 <br>\n";
    ?>

    This is the output:

    Original String: abcDef123.iuj*234_ki:sekKKK&HI^%kmn m,@#3K
    Only letters/numbers: abcDef123iuj234kisekKKKHIkmnm3K
    Only Everything else: .*_:&^% ,@#

    Note in the preg_replace line, the ^ symbol means NOT ... so it's in
    one line, but not the other ... the compliment, so to speak.


  6. #5
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    Ahh that's what I was looking for - thanks!
    tekp :cheeky: tekponline.com


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:00 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com