简体   繁体   English

php | 通过正则表达式在数组中查找

[英]php | find in array by regular expression

I'm looking the way to find one string in array of strings: 我正在寻找在字符串数组中找到一个字符串的方法:

$array = ['string1'=>'custom', 'other string'=>'another', 'another'=>'again', 'custom'=>'pid|100', 'once again'=>'pid|'];

I need to find this element: pid|100 ; 我需要找到这个元素: pid | 100 ;

String should containt: pid|any positive integer number 字符串应该包含: pid |任何正整数

foreach($array as $value) { 
   // regular expression here
}

How can I found this value? 我怎样才能找到这个价值? Thanks! 谢谢!

你想使用preg_grep :)

print_r(preg_grep('/^pid\|[0-9]+$/',$array));

in_array(preg_match('/^pid\\|[0-9]+$/',$your_array),$your_array);

OR 要么

in_array(preg_grep('/^pid\\|[0-9]+$/',$your_array),$your_array);

if you need to check this particular element ie pid|100 then just use in_array('pid|100',$your_array) there is no need to check using regular expression 如果你需要检查这个特定元素,即pid|100那么只需使用in_array('pid|100',$your_array)就不需要使用正则表达式来检查

hope this will help you in solve your problem 希望这能帮助您解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM