0

How would I improve this functions for that is: Searches an array for fields names which either:

  • a) Exactly matches
  • b) beings with "_" check if string begins with
  • c) ends with "_" check if string ends with

E.g I have a list of column names:

array(
    'customer_name',
    'customer_lastname',
    'customer_streetname',
    'customer_dob',
    'system_modified'
)

And another array with formatting conditions:

array(
    '_dob' => 'date_dob',
    '_name' => 'varchar',
    'customer_name' => 'html_text',
    'system_' => 'required'
)

Results apply the conditions against the column names:

1. customer_name = html_text (exact matches have higher preference)
2. customer_lastname = varchar
3. customer_streetname = 
4. customer_dob = dob
5. system_modified = required

Current have this:

protected function matchPatterns($string) {
    $return = array();
    $parrerns = $this->_normaliseArrayItem($this->getPatterns());
    foreach ($parrerns as $match) {

        // If exact match
        if($string == $match) {
            $return[] = $match;
            break;
        // Else if begins with _ and ends with string.
        } elseif($string[0] == "_" && substr_compare($string, $match, -strlen($match), strlen($match)) === 0) {
            $return[] = $match;
        }

    } // end loop
    return $return;
}


/**
 * Return an array of validation patterns.
 *
 * @return string[]
 */
public function getPatterns() {
    return $this->_patterns;
}


/**
 * Returns an item as array rather than single item.
 *
 * @param string[] $data
 * @return string[]
 */
protected function _normaliseArrayItem($data) {
    if(!isset($data[0])|| !is_array($data)) {
        $tmp[] = $data;
        $data = $tmp;
    }
    return $data;
}
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
John Magnolia
  • 16,769
  • 36
  • 159
  • 270
  • you can use `==` or `===` for exact match rtim and ltrim, substr there is no need of regx i thing other way is better – NullPoiиteя Jan 16 '13 at 17:13
  • http://php.net/manual/en/function.preg-match.php – Blazemonger Jan 16 '13 at 17:14
  • see http://stackoverflow.com/questions/2160210/compare-portion-of-the-string-using-php?lq=1 and http://stackoverflow.com/questions/1962031/how-to-check-if-a-string-starts-with-in-php?rq=1 – Rachel Gallen Jan 16 '13 at 17:15
  • `customer_dob = dob` How do you get to that if the value to `_dob` is `date_dob` (and not `required`). It's hard to understand your logic. Maybe you could describe or what you are doing here and eventually try to find another solution to your problem. – feeela Jan 16 '13 at 17:16
  • Why are you returning an array? The expected results seem to be just a single formatting condition for each item. – Barmar Jan 16 '13 at 17:22

2 Answers2

0

Adapted from Marc's solution.

if(substr($str, 0, 1) === '_' || substr($str, -1) === '_') {
    // it starts or ends with an underscore
} else if($str == $match) {
    // it's the same
}

I don't entirely understand your question though.

David Harris
  • 2,697
  • 15
  • 27
  • regex just to look at the first char of a string is rather heavy-duty.. why not `substr($str,0,1) == '_'`, `or $str[0] == '_'` – Marc B Jan 16 '13 at 17:14
  • they are, but for simple things they have rather massive overhead. – Marc B Jan 16 '13 at 17:16
0
foreach ($parrerns as $match => $format) {
    // If exact match
    if ($string == $match) {
        $return[] = $format;
        break;
    // Else if begins with _ and string ends with it.
    } elseif ($match[0] == "_" && substr_compare($string, $match, -strlen($match), strlen($match)) === 0) {
        $return[] = $format;
    // Else if ends with _ and string begins with it
    } elseif (substr($match, -1) == "_" && substr_compare($string, $match, 0, strlen($match)) == 0) {
        $return[] = $format;
    }
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Barmar
  • 741,623
  • 53
  • 500
  • 612