Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 34

Thread: Help with a preg_match

  1. #21
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    like this?

    Code:
        preg_match_all ('/(<a href=\")(.*?)(\/\"|\")/i', $result, $matches);
        for ($i=0;$i<sizeof($matches[2]);$i++)
            {
             if (basename($matches[2][$i]) == basename($links_full_url))
                {
                echo "found on link $i<br>";
                break;
                }
            }
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #22
    Join Date
    Dec 2008
    Posts
    48
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    no, preg_match already stores the URLs in the array without the variables

    The array also seems to be broken when print_r() so i guess the "&" from the URLs is braking it?

  3. #23
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    It is working for me. When using print_r() try using your browser to view the source. When you do you will see that the results are a little bit different. You will also notice that the match is found.

    Here is the script I was working with:
    PHP Code:
    <pre><?php
    $links_full_url
    ="http://www.site.com/OtherMethods.php?error=1&whatever=2";
    $g=basename($links_full_url);
    $result '<strong></strong><font face="Verdana" size="2"><br> 
    » </font><strong><a href="http://www.site.com/OtherMethods.php?error=1&whatever=2"><font face="Verdana" size="2">Other 
    Methods</font></a></strong>'
    ;
        
    preg_match_all ('/(<a href=\")(.*?)(\/\"|\")/i'$result$matches);
        for (
    $i=0;$i<sizeof($matches[2]);$i++)
            {
             if (
    basename($matches[2][$i]) == basename($links_full_url))
                {
                echo 
    "found on link $i<br>";
                break;
                }
            }
    print_r($matches);
    ?></pre>
    To choose the lesser of two evils is still to choose evil. My personal site

  4. The Following User Says Thank You to james438 For This Useful Post:

    nicmo (11-28-2010)

  5. #24
    Join Date
    Dec 2008
    Posts
    48
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    thanks james its working, i had the wrong preg_match for god knows why...

    I started testing and after 4 positive sit checks i got my first problem.

    Code:
    <a class="fadeThis " href="http://www.site.com/OtherMethods.php">
    because if the class, it is not found. Also, some sites use:

    Code:
    <a
    href="http://www.site.com/OtherMethods.php">
    No space between a and href, just a line break. Also cannot be found ofcourse. I guess this can be done by adding some extras to the preg_match, could you help me once more? :P

  6. #25
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    My goal is to try and keep the preg_match_all simple as it is with most scripting. We can get more complicated as needed. For now try replacing

    PHP Code:
        preg_match_all ('/(<a href=\")(.*?)(\/\"|\")/i'$result$matches); 
    with
    PHP Code:
        preg_match_all ('/(href=\")(.*?)(\/\"|\")/i'$result$matches); 
    To choose the lesser of two evils is still to choose evil. My personal site

  7. #26
    Join Date
    Dec 2008
    Posts
    48
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    would you have any other suggestion to try and keep the "<a "? I really need to make sure its a proper link else people will cheat my system.

  8. #27
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    how about
    PHP Code:
        preg_match_all ('/(<a.*?href=\")(.*?)(\/\"|\")/i'$result$matches); 
    To choose the lesser of two evils is still to choose evil. My personal site

  9. The Following User Says Thank You to james438 For This Useful Post:

    nicmo (11-29-2010)

  10. #28
    Join Date
    Dec 2008
    Posts
    48
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    that does work with classes and onclicks, etc. However sites that for some reason have their HTML displayed like this site: http://www.secnews.gr/ the preg_match cant find any links. Does .*? include "" or line breaks?

    Code:
    <a
    href="http://www.site.com/OtherMethods.php">

  11. #29
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    try this
    PHP Code:
        preg_match_all ('/(<a.*?href=\")(.*?)(\/\"|\")/is'$result$matches); 
    In the pcre that we have been working with thus far we have been using the i modifier, which makes the pcre case insensitive. The s modifier will tell the pcre to capture newlines with the dot metacharacter. For whatever reason, the newline is a whitespace character that the dot metacharacter will not capture by default.

    sorry about earlier. when you were saying linebreaks earlier I thought you were talking about <br>
    Last edited by james438; 11-29-2010 at 03:59 PM. Reason: added last sentence.
    To choose the lesser of two evils is still to choose evil. My personal site

  12. The Following User Says Thank You to james438 For This Useful Post:

    nicmo (11-29-2010)

  13. #30
    Join Date
    Dec 2008
    Posts
    48
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Thats awsome James, you saved my bacon regex is a little too much info for me right now, hopefully i will grasp it soon.

    So far, all the tests are working.

Bookmarks

Posting Permissions

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