溫馨提示×

php stristr 是否支持數組操作

PHP
小樊
85
2024-11-19 08:13:34
欄目: 編程語言

PHP 的 stristr 函數不支持數組操作

例如,如果您有一個字符串數組,并希望找到每個字符串中第一個出現的某個子字符串,您可以這樣做:

$strings = ['hello world', 'goodbye world', 'hello PHP'];
$search = 'world';

$result = array_map(function($string) use ($search) {
    return stristr($string, $search);
}, $strings);

print_r($result);

這將輸出:

Array
(
    [0] => world
    [1] => world
    [2] => 
)

在這個例子中,我們使用了 array_map 函數來遍歷數組中的每個字符串,并將 stristr 應用于每個字符串。use 關鍵字用于將外部變量 $search 傳遞給匿名函數。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女