Pattern Matching When $haystack _may_ be an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jerrygarciuh

    Pattern Matching When $haystack _may_ be an array

    Hello,

    I am converting a large scale app from Perl TemplateToolkit and
    Class::DBI to PHP.

    TemplateToolkit has elegant ways of coping with variables that may be
    a scalar or may be an array. So my predecessor has a number of vars
    that are ambiguous in this way.

    My question to you is whether, given this ambiguity, there is a better
    way to do this in PHP than my current solution?

    // $a is sometimes an array
    $a = array('foo', 'bar', 'kermit ruffins');

    if (is_array($a)) {
    foreach($a as $b) {
    if (preg_match('/ruffin/', $b)) {
    echo 'ruffin found';
    }
    }
    } else {
    if (preg_match('/ruffin/', $a)) {
    echo 'ruffin found';
    }
    }

    Much obliged for any help!

    Peace,

    JG

  • ZeldorBlat

    #2
    Re: Pattern Matching When $haystack _may_ be an array

    On Jun 8, 1:33 pm, jerrygarciuh <jerrygarc...@g mail.comwrote:
    Hello,
    >
    I am converting a large scale app from Perl TemplateToolkit and
    Class::DBI to PHP.
    >
    TemplateToolkit has elegant ways of coping with variables that may be
    a scalar or may be an array. So my predecessor has a number of vars
    that are ambiguous in this way.
    >
    My question to you is whether, given this ambiguity, there is a better
    way to do this in PHP than my current solution?
    >
    // $a is sometimes an array
    $a = array('foo', 'bar', 'kermit ruffins');
    >
    if (is_array($a)) {
    foreach($a as $b) {
    if (preg_match('/ruffin/', $b)) {
    echo 'ruffin found';
    }
    }
    } else {
    if (preg_match('/ruffin/', $a)) {
    echo 'ruffin found';
    }
    }
    >
    Much obliged for any help!
    >
    Peace,
    >
    JG
    Just make it an array if it isn't then handle it as an array
    everytime:

    // $a is sometimes an array
    $a = array('foo', 'bar', 'kermit ruffins');

    if(!is_array($a ))
    $a = array($a);

    foreach($a as $b) {
    if (preg_match('/ruffin/', $b)) {
    echo 'ruffin found';
    }
    }

    Comment

    • jerrygarciuh

      #3
      Re: Pattern Matching When $haystack _may_ be an array

      Well I could do that except that it would mean re-writing dozens more
      script than I currently have to. I was more looking to see if anyone
      had a more elegant solution than the one I posted.

      Thanks for the reply!

      JG



      On Jun 8, 3:39 pm, ZeldorBlat <zeldorb...@gma il.comwrote:
      On Jun 8, 1:33 pm, jerrygarciuh <jerrygarc...@g mail.comwrote:
      >
      >
      >
      Hello,
      >
      I am converting a large scale app from Perl TemplateToolkit and
      Class::DBI to PHP.
      >
      TemplateToolkit has elegant ways of coping with variables that may be
      a scalar or may be an array. So my predecessor has a number of vars
      that are ambiguous in this way.
      >
      My question to you is whether, given this ambiguity, there is a better
      way to do this in PHP than my current solution?
      >
      // $a is sometimes an array
      $a = array('foo', 'bar', 'kermit ruffins');
      >
      if (is_array($a)) {
      foreach($a as $b) {
      if (preg_match('/ruffin/', $b)) {
      echo 'ruffin found';
      }
      }
      } else {
      if (preg_match('/ruffin/', $a)) {
      echo 'ruffin found';
      }
      }
      >
      Much obliged for any help!
      >
      Peace,
      >
      JG
      >
      Just make it an array if it isn't then handle it as an array
      everytime:
      >
      // $a is sometimes an array
      $a = array('foo', 'bar', 'kermit ruffins');
      >
      if(!is_array($a ))
      $a = array($a);
      >
      foreach($a as $b) {
      if (preg_match('/ruffin/', $b)) {
      echo 'ruffin found';
      }
      >
      }

      Comment

      Working...