SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fastest Extract of String Data

    Hello I am trying to figure out the fastest way to extract data from these urls like so:

    PHP Code:
    $test1 "http://www.blah.com/1.php?siteID=1244&page=blah";
    $test2 "http://www.blah.com/1.php?page=blah&siteID=1244";
    $test3 "http://www.blah.com/1.php?page=blah&siteID=1244&other=no"
    All these links contain "siteID=" but all in different locations, I am trying to extract the 1244 out of the url so anything after siteID= that number is what I need.

    I am having problems getting it work work since siteID shows up in any different locations and sometimes but not always is the end of the string.

    Does anyone have some ideas of what I could try to extract that info out of that string.

    Thanks in advance!

  2. #2
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    preg_match '/.*siteID=(\d+).*/' ), $url$site_id );

    echo 
    $site_id[1]; 
    Should work. I gave it a brief test.

  3. #3
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    preg_match '/.*siteID=(\d+).*/' $url$site_id ); 
    print_r($site_id); 
    I tried just printing out the array ($site_id) but it came out blank.

    I messed around with it some more I could not get it to save the siteID

  4. #4
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had an extra ) in my original code, which is why it didn't work. My apologies. I actually tested this in PHP instead of a regex tester, so I know this works:

    PHP Code:
    $url "http://www.blah.com/1.php?siteID=1244&page=blah";
    preg_match '/.*siteID=(\d+).*/'$url$site_id );

    echo 
    $site_id[1]; 

  5. #5
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I cant seem to get it to work, I get this:

    Notice: Undefined offset: 1 on line 167 which is

    echo $site_id[1];

    Im not sure how to fix it because its not working as of right now, might be because of that notice thou.

  6. #6
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems the problem is not present in this code:

    PHP Code:
        for($i=0;$i count($parsed);$i++)
        {
            echo 
    $parsed[$i]."-";
            
    $url "http://www.blah.com/1.php?siteID=1244&page=blah";
            
    preg_match '/.*siteID=(\d+).*/'$url$site_id );
            echo 
    $site_id[1];
        } 
    The problem is that $parsed[$i] = $url that I passed into it. The url I copy and pasted the output directly from the browser so when I have this code is when I get the error:

    PHP Code:
        for($i=0;$i count($parsed);$i++)
        {
            echo 
    $parsed[$i]."-";
            
    $url $parsed[$i];
            
    preg_match '/.*siteID=(\d+).*/'$url$site_id );
            echo 
    $site_id[1];
        } 
    Now here $url is equal to what it should be the exact string is passed in and it gives me the error.

  7. #7
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does..
    PHP Code:
    var_dump($url); 
    ..output?

  8. #8
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    string(47) then the URL

  9. #9
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah I think I found the problem

    Some urls its not always siteID its siteid and otheres its siteID, yours seems to be case sensitive.

    Also I noticed a few urls didnt have siteid in it which would by why its spitting out an error. Is there a way to have it return false if theres no siteid in it and have it not be case sensitive.

    Thanks again!

  10. #10
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach($parsed as $url) {
        echo 
    $url "-";
        if(
    preg_match ('/(?<=siteid=)\d+/i'$url$site_id)) {
            echo 
    $site_id[0];
        }


  11. #11
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awsome man thank you so much that work perfect!

  12. #12
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, have a look at parse_str.

    =Austin
    Austin Hastings - Principal Consultant - Longacre, Inc.

    Anything you can do, you can do better.


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
  •