PHP Help? Gurus, hook a brother up...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RayVega
    Confirmed User
    • Jul 2004
    • 4212

    #1

    PHP Help? Gurus, hook a brother up...

    Been a while since I wrote code and I'm working on a small script and sadly I'm stuck on what should be a relatively easy regular expression match in PHP.

    I have a variable with a bunch of text and need to match the text between two different patterns and assign it to a variable.

    Ex.
    $a="...$agentid=1010303&listingid...etc.";

    I need to grab text between $agentid= and &listingid and assign to variable so I am left with $num="1010303". Now, I have to do some things with that number but first need to extract it from the variable.

    Rather than sift through web pages and using trial and error, I figure one of you guys can spew it out off the top of your head.

    Any ideas?
    Ray "The Don" Vega

    Managing Director
    Private Equity Fund

    [email protected]
  • drocd
    Confirmed User
    • Aug 2007
    • 128

    #2
    PHP Code:
    $str = '$agentid=1010303&listingid';
    if(preg_match_all('/agentid\=(\d+)&listingid/',$str,$matches)){
        $num = $matches[1][0];
    } 
    
    230-699

    Comment

    • RayVega
      Confirmed User
      • Jul 2004
      • 4212

      #3
      Originally posted by drocd
      PHP Code:
      $str = '$agentid=1010303&listingid';
      if(preg_match_all('/agentid\=(\d+)&listingid/',$str,$matches)){
          $num = $matches[1][0];
      } 
      
      Thanks bro...right on target. problem solved!
      Ray "The Don" Vega

      Managing Director
      Private Equity Fund

      [email protected]

      Comment

      • nation-x
        Confirmed User
        • Mar 2004
        • 5370

        #4
        holy crap! I think this is the first time I have seen a regex posted on gfy... lol

        Comment

        • mrkris
          Confirmed User
          • May 2005
          • 2737

          #5
          Originally posted by nation-x
          holy crap! I think this is the first time I have seen a regex posted on gfy... lol
          Really? Let me add another. drocd should remember this regex I pasted him a while ago.

          PHP Code:
          @ox.+ref = "(.+)".+c="(.+)".+e="(.+)".+ft">(.+)<@Us 
          

          PHP-MySQL-Rails | ICQ: 342500546

          Comment

          • GrouchyAdmin
            Now choke yourself!
            • Apr 2006
            • 12085

            #6
            Originally posted by mrkris
            Really? Let me add another. drocd should remember this regex I pasted him a while ago.

            PHP Code:
            @ox.+ref = "(.+)".+c="(.+)".+e="(.+)".+ft">(.+)<@Us 
            
            Rails is for snails.

            Here, have a link scraper.

            Code:
            $preg = "/a[\s]+[^>]*?href[\s]?=[\s\"\']+(.*?)[\"\']+.*?>([^<]+|.*?)?<\/a>/i";

            Comment

            • Bird
              Confirmed User
              • Jan 2005
              • 4365

              #7
              Whats wrong with
              $num = $a['agentid']
              ICQ:268731675

              Comment

              • rowan
                Too lazy to set a custom title
                • Mar 2002
                • 17393

                #8
                As Bird suggested, if those variables are actually part of the URL that is running the script then it's easy peasy... $num = $_GET["agentid"]; (replace GET with POST if it's a form)

                Comment

                • GrouchyAdmin
                  Now choke yourself!
                  • Apr 2006
                  • 12085

                  #9
                  Originally posted by rowan
                  As Bird suggested, if those variables are actually part of the URL that is running the script then it's easy peasy... $num = $_GET["agentid"]; (replace GET with POST if it's a form)
                  Why does everybody hate $_REQUEST?

                  I was praying that's what Bird meant, and not eval() parse_str($_SERVER["QUERY_STRING"]). You can only imagine what kind of shit I've seen to even assume such a crazy thing

                  Comment

                  • rowan
                    Too lazy to set a custom title
                    • Mar 2002
                    • 17393

                    #10
                    Originally posted by GrouchyAdmin
                    Why does everybody hate $_REQUEST?
                    Just habit from writing scripts that are to be used by several people. Keep it more specific.

                    Some webmasters will do odd things, like a javascript call that redirects to your site by POSTing to it. (The reason for this? It blocks/clears the referer.)

                    Comment

                    • V_RocKs
                      Damn Right I Kiss Ass!
                      • Nov 2003
                      • 32449

                      #11
                      Originally posted by RayVega

                      I have a variable with a bunch of text and need to match the text between two different patterns and assign it to a variable.
                      Originally posted by GrouchyAdmin
                      Why does everybody hate $_REQUEST?
                      Why does everyone hate reading the question?

                      Comment

                      • borked
                        Totally Borked
                        • Feb 2005
                        • 6284

                        #12
                        Originally posted by RayVega
                        Thanks bro...right on target. problem solved!
                        although, cos I know what you're doing the agent id is a 10 digit string and will only occur once, so to be absolutely sure....

                        if(preg_match('/agentid\=(\d{10})&listingid/',$str,$matches)){

                        cos otherwise, the regexp given would pick up also:

                        $a="...$agentid=1&listingid...etc.";

                        <- my invoice for this wonderful piece of insight
                        Last edited by borked; 01-14-2009, 10:30 PM.

                        For coding work - hit me up on andy // borkedcoder // com
                        (consider figuring out the email as test #1)



                        All models are wrong, but some are useful. George E.P. Box. p202

                        Comment

                        • GrouchyAdmin
                          Now choke yourself!
                          • Apr 2006
                          • 12085

                          #13
                          Originally posted by V_RocKs
                          Why does everyone hate reading the question?
                          You've done two things wrong for the adult industry:

                          1) Assuming he has the variable defined locally.
                          2) Assuming he knows what a variable is.

                          Comment

                          • tranza
                            ICQ: 197-556-237
                            • Jun 2003
                            • 57559

                            #14
                            This site maybe can help you:
                            www.pixel2life.com
                            I'm just a newbie.

                            Comment

                            Working...