Need help with PHP code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DigitalPimp
    Confirmed User
    • Jun 2003
    • 512

    #1

    Need help with PHP code

    Anyone know why the following does not return desired results and how to get it to?

    Code:
    $string = 'a,b,c,d';
    
    if(eregi("(a)|(adf)|(c)",$string,$matchedtext)){ 
    echo $matchedtext[1]; // This returns an "a" which I want
    echo $matchedtext[2]; // This is empty, I want it to return a "c"
    }
  • DigitalPimp
    Confirmed User
    • Jun 2003
    • 512

    #2
    Code should have read:

    Code:
    $string = 'a,b,c,d';
    
    if(eregi("(a)|(c)|(adf)",$string,$matchedtext)){ 
    echo $matchedtext[1]; // This returns an "a" which I want
    echo $matchedtext[2]; // This is empty, I want it to return a "c"
    }

    Comment

    • CS-Jay
      Confirmed User
      • Oct 2003
      • 1794

      #3
      who don't you do a print_r($matchedtext) to see what's in there?
      I do stuff - aIm CS_Jay_D

      Comment

      • Beaver Bob
        Confirmed User
        • Aug 2005
        • 1099

        #4
        not sure exactly what you are trying to do here, but take some time to read up on regular expressions in PHP and you should be able to figure it out.

        http://www.regular-expressions.info/php.html
        http://www.php.net/manual/en/function.ereg.php
        Spunky Dollars | Need Content?
        Paysite Owners: Protect Your Members Area with Strongbox

        361-574-229

        Comment

        • DigitalPimp
          Confirmed User
          • Jun 2003
          • 512

          #5
          Originally posted by CS-Jay
          who don't you do a print_r($matchedtext) to see what's in there?
          print_r($matchedtext) returns:

          Array ( [0] => a [1] => a [2] => [3] => )

          My understanding is matchedtext[0] is supposed to return the complete string matched, matchedtext[1] is supposed to return the substring which starts at the first left parenthesis, matchedtext[2] the next and so on. For some reason matchedtext[2] is not returning the letter "c" as I would expect.

          Comment

          • DigitalPimp
            Confirmed User
            • Jun 2003
            • 512

            #6
            Originally posted by Beaver Bob
            not sure exactly what you are trying to do here, but take some time to read up on regular expressions in PHP and you should be able to figure it out.

            http://www.regular-expressions.info/php.html
            http://www.php.net/manual/en/function.ereg.php
            I am trying to get the second substring "(c)" to get stored in the $matchedtext array. I will review those pages.

            Comment

            • calmlikeabomb
              Confirmed User
              • May 2004
              • 1323

              #7
              You owe me a blow job.

              It could be shorter, but that takes time, more thought and money.

              PHP Code:
              <?php
              
              $string = 'a,b,c,d';
              
              foreach(explode(',', $string) as $match) {
                  if(in_array($match, array('a','c'))) $matches[] = $match;
              }
              
              print_r($matches); // Here are your items.
              
              ?>
              Last edited by calmlikeabomb; 05-27-2009, 05:41 PM.
              subarus.

              Comment

              • DigitalPimp
                Confirmed User
                • Jun 2003
                • 512

                #8
                thank you!

                Comment

                Working...