Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php remove emails and web address from text

Options
  • 24-07-2006 4:57pm
    #1
    Closed Accounts Posts: 1,200 ✭✭✭


    I have user adding content and description to a website, but I would like to see a way of checking if they submit web and email addresses included into the textarea form and remove it if any.

    Anybody has any ideeas how?


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    I wouldn't know the code to use off, but you could simply check for anything that falls into the format of name@domain.com or www.domain.com in the text area. You could steal the email check from email validation scripts, there's plenty of good format checkers around for free!


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    i can not use the email validators because they check a string to make sure it has the @ and the . after, making it a valid email address.

    i need to look for the entire string and if found, remove it from the post


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    louie wrote:
    i can not use the email validators because they check a string to make sure it has the @ and the . after, making it a valid email address.
    You can use a well written regexp to match email addresses, you should be able to find one on php.net.

    There are a few simple rules for email addresses which make them easy enough to isolate from normal text:
    1. They never contain spaces
    2. There are certain characters that they cannot contain (!, " and ^ to name but three)

    4. They always end with a dot, followed by a domain name which is no less than 2 and no more than 4 characters long.

    Even just building a regexp based on these rules, you're unlikely to generate any false postives.

    A regexp for matching web addresses may take more effort, but it should be possible in the end.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Thats the kind of thing I was talking about, searching the text area for a string in the specific email format. Most of the rules apply similarly to web addresses so it should be possible too.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    i spent more then an hour on google looking for all posibilities and found nothing. I remember doing something similar using asp, few years back, but i can not find it now and no samples availalble in php.

    I keep looking arround but doesn't seem to get lucky or i am not looking for the right thing.


  • Advertisement
  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    This looks like exactly what you want for the email part, should be easily edited to run automatically:

    [php]<?php

    ###############################################################
    # Email Extractor 1.0
    ###############################################################
    # Visit http://www.zubrag.com/scripts/ for updates
    ###############################################################
    ?>

    <form method="post">
    Please enter full URL of the page to parse (including http://):<br />
    <input type="text" name="url" size="65" /><br />
    or enter text directly into textarea below:<br />
    <textarea name="text" cols="50" rows="15"></textarea>
    <br />
    <input type="submit" value="Parse Emails" />
    </form>

    <?php
    if (isset($_REQUEST) && !empty($_REQUEST)) {
    // fetch data from specified url
    $text = file_get_contents($_REQUEST);
    }
    elseif (isset($_REQUEST) && !empty($_REQUEST)) {
    // get text from text area
    $text = $_REQUEST;
    }

    // parse emails
    if (!empty($text)) {
    $res = preg_match_all(
    "/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
    $text,
    $matches
    );

    if ($res) {
    foreach(array_unique($matches[0]) as $email) {
    echo $email . "<br />";
    }
    }
    else {
    echo "No emails found.";
    }
    }

    ?>[/php]


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    thanks for the example.
    using that and make few modifications to suit me i came up with this if anybody ever needs it:

    [PHP]// parse emails
    if (!empty($text)) {
    //match emails
    $res_email = preg_match_all("/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",$text,$matches);

    //clean email if found
    if ($res_email) {
    $i = 0;
    foreach(array_unique($matches[$i]) as $email) {
    $text = str_replace($email,"",$text);
    $i++;
    }
    }
    //match web addresses
    $res_www = preg_match_all("/[a-z0-9]+([a-z0-9]+)*.([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i", $text, $matches);
    //clean web addresses if found
    if ($res_www) {
    $i = 0;
    foreach(array_unique($matches[$i]) as $www) {
    $text = str_replace($www,"",$text);
    $i++;
    }
    }

    echo "<br />".$text;
    }[/PHP]


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    No problem!


Advertisement