Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 34

Thread: Help with a preg_match

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

    Default

    I notice you are still using your pcre as opposed to the ones bluewalrus and myself suggested. At least try not to use the pipe: | as a delimiter. The pipe has special meaning in pcre as the OR operator.
    To choose the lesser of two evils is still to choose evil. My personal site

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

    Default

    Quote Originally Posted by james438 View Post
    I notice you are still using your pcre as opposed to the ones bluewalrus and myself suggested. At least try not to use the pipe: | as a delimiter. The pipe has special meaning in pcre as the OR operator.
    how do i use this?

    Code:
    "/<a href=\"(.*?)"/i"
    where do i tell it what to look for.

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

    Default

    Don't use it. It does not work. See my earlier posts and take a closer look at this post
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #14
    Join Date
    Dec 2008
    Posts
    48
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by james438 View Post
    Don't use it. It does not work. See my earlier posts and take a closer look at this post
    isnt that just doing what basename() does?

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

    Default

    Code:
    preg_match ("|<[aA] (.+?)".basename($full_url)."(.+?)>(.+?)<\/[aA]>|i", $result, $matches);
    when you insert basename($full_url) into a pcre like the above example you are actually looking for the exact phrase "basename" followed by the capture that you have named "full_url". To my knowledge php can't be inserted into pcre in the way that you are trying to do. With some modifiers you can insert php into your second part, $result, but that is something different.

    "/<a href=\"(.*?)"/i" does not work for a couple reasons. The most obvious is that the second double quote is not escaped so that it is not recognized as a literal double quote. It should be "/<a href=\"(.*?)\"/i"

    The other problem is that it is too simple and only accounts for very limited types of inputs. He is, however on the right track. I also know bluewalrus has a fair amount of experience with pcre as well.

    The example script I gave you in an earlier post should find what you are looking for and put it into the variable $found. With $found compare it to your basename($full_url).

    For example try adding the following:

    Code:
    $basename_url=basename($full_url);
    if ($found=="$basename_url") echo "they matched";
    It should look something like this:
    PHP Code:
    <?php
    $result 
    '<strong></strong><font face="Verdana" size="2"><br> 
    » </font><strong><a href="OtherMethods.php"><font face="Verdana" size="2">Other 
    Methods</font></a></strong>'
    ;
    preg_match ('/(<a href=\")(.*?)(\?|\/\"|\")/i'$result$matches);
    $match=$matches[2];
    if (
    substr($match,-1,1)=='/'$match=substr_replace($match,'',-1,1);
    $match=explode("/","$match");
    $found=end($match);
    $full_url='http://www.site.com/OtherMethods.php';
    $basename_url=basename($full_url);
    print 
    $found;
    if (
    $found=="$basename_url") echo "<br>they matched";
    else echo 
    "<br>not found";
    ?>
    Last edited by james438; 11-29-2010 at 03:51 PM. Reason: changed "he" to "bluewalrus"
    To choose the lesser of two evils is still to choose evil. My personal site

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

    nicmo (11-25-2010)

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

    Default

    it wont work, $result is a full html page with many links pulled by curl. I need something that looks inside all links looking for my basename() thats what my broken preg_match does.

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

    Default

    just change preg_match to preg_match_all

    However, now you will need to put $match into a loop of some sort, probably a "while" loop applying
    Code:
    $full_url='http://www.site.com/OtherMethods.php'; 
    $basename_url=basename($full_url); 
    if ($found[]=="$basename_url") echo "<br>they matched"; 
    else echo "<br>not found";
    to each part of the array. The above will need to be modified slightly.

    If you are having trouble with this let me know.
    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-27-2010)

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

    Default

    hey james this is almost working. Right now only fails on links that contain variables.

    Like: "http://www.site.com/OtherMethods.php?error=1&whatever=2"

    preg match only saves "http://www.site.com/OtherMethods.php?" from that URL, anyway to keep the entire URL?

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

    Default

    can I see what the script you are using currently looks like? Remember, preg_match will only pull the first result it finds and that's it. preg_match_all will pull all of the results and store it into a multidimensional array, which I am not a big fan of pulling results from to be honest, but it all depends on your needs.

    The script I presented only pulls
    PHP Code:
    OtherMethods.php?error=1&whatever=
    from
    Code:
    http://www.site.com/OtherMethods.php?error=1&whatever=2
    which is what I thought you wanted. If you want the entire url, I can probably write something up for you, but it will be different than what I was writing. I thought you specifically wanted the basename($full_url) as opposed to the full url for comparison purposes... Sorry about the lack of understanding of what you want on my part.
    Last edited by james438; 11-28-2010 at 02:00 PM. Reason: spelling and fixed my coding error that was in the first example above.
    To choose the lesser of two evils is still to choose evil. My personal site

  12. #20
    Join Date
    Dec 2008
    Posts
    48
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    PHP 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;
                }
            } 
    Where $result is some site full HTML source, via curl.

    This is what is working for me but i have that variables problem. I do want the basename() or whatever is after the last "/", basename() on" http://www.site.com/OtherMethods.php?error=1&whatever=2" still returns "OtherMethods.php?error=1&whatever=2" and thats what i need to look for because some sites use wordpress or other CMS and their linking systems use variables to find the right pages.

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
  •