Results 1 to 2 of 2

Thread: ereg Help for A Beginner

  1. #1
    okason Guest

    ereg Help for A Beginner

    Hello all, I am taking a class on PHP and I need help with the question below


    Use the ereg() function and regular expressions to find the web address http://www.google.com/
    in the following string and echoes it to the browser. Below is the string:
    <a href="http://www.google.com/">Link to Google</a>
    Do not use the complete url string "http://www.google.com/" in your search expression.

    You must use some combination of search expressions such as a-z, A-Z, etc.
    Be careful to account for the double-quotes in the string.
    You will probably need to use the backslash character as an
    scape character in order to process the string properly.



    Here is my attempt to answer the question but for some reason the string variable $givenurl is not grabbing the whole string, so I need help particularly with how to properly escape the string <a href="http://www.google.com/">Link to Google</a>

    <?php
    $givenurl = "<a href\=\"http:\/\/www.google.com/\">Link to Google</a>";

    if ($givenurl !== "")

    {
    ereg("[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]", $givenurl, $newurl);
    echo $newnurl;
    }

    else

    echo "No Url found";


    ?>

  2. #2
    Join Date
    May 2010
    Location
    College Station, TX
    Posts
    216
    As far as I can see, your regex pattern shouldn't match that candidate string at all because there's no "@" symbol in the candidate string. In fact your regex pattern looks like a rudimentary email detection pattern. Try this instead:

    http:\/\/(www.)?[a-zA-Z0-9\-_.]+\.[a-zA-Z]{3,4}

    Tested and verified with following code.

    PHP Code:
    $haystack "<a href=\"http://www.google.com\">Link to Google</a>";
    ereg("http:\/\/(www.)?[a-zA-Z0-9\-_.]+\.[a-zA-Z]{3,4}"$haystack$result);

    if( 
    $result )
        echo 
    $result[0];
    else
        echo 
    "Fail!"
    All web designers hate the internet. If I spend all day making/updating/looking at websites, why the hell would I want to deal with it outside of work?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •