Surely there's a function for this. But I can't find it. I need to search for a string.... plenty of those of all flavors, and when I find it I need to capture the following x characters...

Examples.... search the string for 'user=' I don't know who the user will be but it's a 8 digit name/number that follows.

I need to capture the info that follows.

other examples..
module=
name ( )

and so on.

Any ideas!

Thanks

    if(preg_match('/(?<=user=).{8,8}/', $string, $matches))
    {
      $user = $matches[0];
    }
    else
    {
       // user not found
    }
    
      NogDog wrote:
      if(preg_match('/(?<=user=).{8,8}/', $string, $matches))
      {
        $user = $matches[0];
      }
      else
      {
         // user not found
      }
      

      Thanks!

      Chuck

        Installer wrote:

        For something more flexible:

        function find_substr($str, $find, $len) {
            if (($pos = strpos($str, $find)) !== false) {
                $result = substr($str, $pos + strlen($find), $len);
            } else {
                $result = 'not found';
            }
            return $result;
        }

        Thanks,

        Chuck

          Perhaps that could be better written as:

          function find_substr($str, $find, $len) {
              if (($pos = strpos($str, $find)) !== false) {
                  $result = substr($str, $pos + strlen($find), $len);
                  if (strlen($result) == $len) {
                      return $result;
                  } else {
                      return false;
                  }
              } else {
                  return false;
              }
          }

          I tend to be suspicious of variables that, on subsequent modification of the code, might suddenly be out of scope due to carelessness.

            Well, nothing as it stands; but laserlight's considering the possibility that altering the code later might result in code paths in which $result is never defined but end with "return $result" anyway (of course, if that happens, it's a bug anyway).
            But hey, there's all sorts of ways the branches could be ordered. Here's another one for the fun of it:

            function find_substr($str, $find, $len)
            {
            	if (($pos = strpos($str, $find)) === false) return false;
            
            $result = substr($str, $pos + strlen($find), $len);
            
            if (strlen($result) < $len) return false;
            
            return $result;
            } 
              NogDog wrote:
              if(preg_match('/(?<=user=).{8,8}/', $string, $matches))
              {
                $user = $matches[0];
              }
              else
              {
                 // user not found
              }
              

              Hey NogDog,

              I was wondering if you could explain the code. It's not working yet (not finding 'user=') and I thought if I understood it more I could trouble shoot it.

              Thanks!

                The expression as given doesn't find "user=", only the eight characters following it. Specifically, it finds the first eight characters (not counting line breaks) that follow an occurrence of "user=".

                  7 days later

                  Well this code is working nicely. And it does well for fixed length data. So now I'm looking for Email=. And email length can vary. I've been checking around for a way to alter the pattern to say take the string until the first blank. I'm see that ability in other functions but I'm not sure how to get that in to the preg_match pattern logic.

                  pointers?

                  Thanks,

                  Chuck

                    The solution will depend on how you can tell where the email address ends. Will there be a whitespace of some kind, a newline character, or something else? If a whitespace, you could do something like:

                    $email = (preg_match('/Email=(\S+)/i', $text, $matches)) ? $matches[1] : NULL;
                    

                    \S+ means "1 to n non-whitespace characters".

                      works great. It comes back with email=xxxxxxxxx.foo.bar.com.

                      I gave it a shot at changing the variables to start reading after the '=' but my brain hurts. I used the string replace to trim email=.

                      Thanks NogDog

                        So now I have a collection of fields coming to me of different lengths. It it easy for sender of the field to make them delineated. So I could use one more turn on this command.

                        lets say the incoming field is like this

                        ....@..&.......texts=blahblah blah blah and blah=etext ....#...%%......

                        where the official Start of info I need is after the = in 'textS=' and the official End of text I need is the last character before the = in '=Etext'.

                        If that string could be isolated and loaded (better with out loading the delineators but I can trim them if needed) that would be very helpful.

                        Thanks!

                        KC

                          Try chaning regular expression from NogDog's example to:

                          /texts=(.*)=etext/

                          It should do what you need. Try to experiment yourself (it will be quicker). To practice you can find some reg-ex tutorial pages. Here's the shortest one I could find (with reg-ex online tester): http://www.quanetic.com/regex.php

                            Write a Reply...