• Remove hyperlinks, e-mails and telephone numbers from a string

    When I was building the internal e-mail system for members to use on MatchMkr, I decided that it’d be best for everyone if website addresses, e-mail addresses and telephone numbers were removed from messages, until users had sent each other at least three messages.

    This way, it would prevent opportunistic members from bombarding users with links to other sites, or simply pestering other members with requests to join them on MSN, or call them on their number.

    The following function does a quick search of the string on-the-fly, and removes the offending items, replacing with text indicating what was removed, without deleting it from the message itself, so that it will appear again, once they have contacted each other the minimum number of times.

    Please note, that I’m only showing you the basic search strings here, as I don’t want to make it too easy for members of MatchMkr to work out how they can bypass the security systems I’ve built into the site!

    If you need more search and replace options, drop me a line.

    The function to clean your strings, removing offending content is as follows:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    function cleanString($my_string) {
    // REMOVE HYPERLINKS, E-MAILS & TELEPHONE NUMBERS
    $my_string = str_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "[WEBSITE]", $my_string);
    $my_string = str_replace("www+://[^<>[:space:]]+[[:alnum:]/]", "[WEBSITE]", $my_string);
    $my_string = str_replace("[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,3})", "[E-MAIL ADDRESS]", $my_string);
    $my_string = str_replace("([0-9](-| |.))?(\(?[0-9]{3}\)?(-| |.)?)?[0-9]{3}(-| |.)?[0-9]{4}", "[TELEPHONE]", $my_string);
    $my_string = str_replace("([0-9](-| |.))?(\(?[0-9]{4}\)?(-| |.)?)?[0-9]{3}(-| |.)?[0-9]{4}", "[TELEPHONE]", $my_string);
    $my_string = str_replace("([0-9](-| |.))?(\(?[0-9]{5}\)?(-| |.)?)?[0-9]{3}(-| |.)?[0-9]{3}", "[TELEPHONE]", $my_string);
    return $my_string;
    }
    Share and Enjoy:
    • Digg
    • Sphinn
    • del.icio.us
    • Facebook
    • Mixx
    • Google Bookmarks

    1 responses to “Remove hyperlinks, e-mails and telephone numbers from a string”


    • Amanda Roberts

      The most comprehensive info I have found on this subject on the net. Will be back soon to follow up.


     Leave a reply