[英]Find character before or after of string using preg_grep() in PHP
我需要使用數組中的PHP preg_grep()
在單詞之前或之后找到任何字符。 我有一個像下面的數組
$findgroup = array("aphp", "phpb", "dephpfs", "potatoes");
我需要從數組中找到具有'php'單詞的值,在單詞'php'之前或之后(任一側,而不是雙方)都帶有一個或兩個字符。 結果應該是數組中的“ aphp”,“ phpb”字。
我嘗試使用以下代碼,但不起作用。
$result[] = preg_grep("/(.{1})php(.{1})/", $findgroup);
錨定您的正則表達式,並在前后添加字符的量詞:
$findgroup = array("aphp", "phpb", "dephpfs", "potatoes", "aphpb", "php");
$result = preg_grep("/^(?:.{1,2}php|php.{1,2})$/", $findgroup);
print_r($result);
輸出:
Array
(
[0] => aphp
[1] => phpb
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.