2016-04-06 13 views
0

私はプラグインを使用していますが、このプラグインは動作していないカスタムフィールドチェックを持っています。特定の条件でカスタムフィールドをチェックするために使用されるプラグインのコードの下。preg_match():デリミタは、英数字またはバックスラッシュでカスタムフィールドチェックを使用しないでください。

<?php if ($custom_field_value != null) { 
    if (($set['condition']['value']['operator'] == 'is' && $set['condition']['value']['value'] == $custom_field_value) 
    || ($set['condition']['value']['operator'] == 'is_not' && $set['condition']['value']['value'] != $custom_field_value) 
    || ($set['condition']['value']['operator'] == 'contains' && preg_match($set['condition']['value']['value'], $custom_field_value)) //The problematic line. 
    || ($set['condition']['value']['operator'] == 'does_not_contain' && !preg_match($set['condition']['value']['value'], $custom_field_value)) 
    || ($set['condition']['value']['operator'] == 'lt' && $set['condition']['value']['value'] < $custom_field_value) 
    || ($set['condition']['value']['operator'] == 'le' && $set['condition']['value']['value'] <= $custom_field_value) 
    || ($set['condition']['value']['operator'] == 'eq' && $set['condition']['value']['value'] == $custom_field_value) 
    || ($set['condition']['value']['operator'] == 'ge' && $set['condition']['value']['value'] >= $custom_field_value) 
    || ($set['condition']['value']['operator'] == 'gt' && $set['condition']['value']['value'] > $custom_field_value)) { 
     $proceed = true; 
    } 
}?> 

問題は、 '含む' ライン内にあり、私のDEBUG.LOGに次のエラーを与える:

PHPの警告:するpreg_match():区切り文字は英数字またはバックスラッシュ

あってはなりませんが

このチェックは、カスタムフィールドに '30'、 'text1'または 'text2'のいずれかが含まれているかどうかを確認するために使用されます。

ここで私は間違っている可能性がありますが、ここでは区切り文字を使用していないと思います。おそらくここで間違っている可能性がありますか?

答えて

0

preg_matchは、この順番でパレメータを期待しています:preg_match($pattern, $string)。また、パターンは、例えば、前記区切り文字として前方スラッシュを使用する。だから、可能な解決策は、次のようになります。

... 
|| ($set['condition']['value']['operator'] == 'contains' 
    && preg_match('/' . $custom_field_value . '/', $set['condition']['value']['value'])) 
|| ($set['condition']['value']['operator'] == 'does_not_contain' 
    && !preg_match('/' . $custom_field_value . '/', $set['condition']['value']['value'])) 
... 

あなたは、文字列は、カスタムフィールド内にある場合は、簡単なチェックを行いたい場合、私はパフォーマンス上の理由からstrposを使用することをお勧めします:

... 
|| ($set['condition']['value']['operator'] == 'contains' 
    && strpos($set['condition']['value']['value'],$custom_field_value) > 0) 
|| ($set['condition']['value']['operator'] == 'does_not_contain' 
    && strpos($set['condition']['value']['value'],$custom_field_value) == FALSE) 
... 
+0

私はこれを読んだとき私はそれがうまくいくと思った、それはない..私はワードプレスのカスタムフィールドが 'Array'を返すことが時々あると信じています。 preg_match()が配列に対して機能しないことがありますか? –

+0

このマニュアルでは、1(見つかったもの)、0(見つからなかったもの)、FALSE(エラーが発生した場合)を返します。 – noreabu

+0

私にとってはエラーが発生しましたが、私は配列だからだと思うが、間違っている可能性がある。私は開発者に連絡して、回答を受け取ったときに更新します。 –

関連する問題