3 或者 $length = 0时,强制使用正则模式,因为取的数量大时,正则速度更快。 * @return array 图片地址的集合数组,若无则返回空数组 */ public static function srcList($data, $startNum = 1, $length = 0, $order = 'asc', $blacklist = false, $model = 'preg'){ if($model === 'preg' || $length > 3 || $length === 0){ $imgSrcArr = self::pregModel($data, [$startNum-1, $length, $blacklist], $order); }else{ $imgSrcArr = []; for($i=$startNum; $i<$startNum+$length; $i++){ $imgSrc = self::strModel($data, $i, $order); if(is_array($blacklist)){ $blackBool = true; foreach ($blacklist as $k=>$v){ if (strpos($imgSrc, $blacklist) !== false) { $blackBool = false; } } if($blackBool){ $imgSrcArr[] = $imgSrc; }else{ $length++; } }else{ if ($blacklist === false || strpos($imgSrc, (string)$blacklist) === false) { $imgSrcArr[] = $imgSrc; }else{ $length++; } } } } return $imgSrcArr; } /** * @param $str * @param $num * @param $order * @return bool|string|null */ public static function strModel($str, $num, $order){ $topStr = null; if($order != 'asc'){ $funcStr = 'strrpos'; }else{ $funcStr = 'strpos'; } for($i=1; $i<=$num; $i++){ $firstNum = $funcStr($str, '/isU", $str, $ereg); $img = $ereg[0]; if($order != 'asc'){ $img = array_reverse($img); }; if(is_array($num)){ $startNum = $num[0]; $length = $num[1]; $blacklist = $num[2]; $imgSrcArr = []; foreach($img as $key=>$value){ $imgSrc = $value; $pregModel='/src=(\'|")(.*)(?:\1)/isU'; preg_match_all($pregModel, $imgSrc, $img1); if(is_array($blacklist)){ $blacklistBool = true; foreach($blacklist as $v){ if(strpos($img1[2][0], $v) !== false){ $blacklistBool = false; }; } if($blacklistBool){ $imgSrcArr[] = $img1[2][0]; }; } else { if ($blacklist === false || strpos($img1[2][0], (string)$blacklist) === false) { $imgSrcArr[] = $img1[2][0]; }; } } if($length > 0){ return array_slice($imgSrcArr, $startNum, $length); }else{ return array_slice($imgSrcArr, $startNum); } }else{ if(!empty($img[$num])){ $imgStr = $img[$num]; $pregModel='/src=(\'|")(.*)(?:\1)/isU'; preg_match_all($pregModel, $imgStr, $img1); return $img1[2][0]; }else{ return null; } } } } /* $str = '

这里是普通文字

这里是干扰元素测试\'\'"""

'; $src = GetImgSrc::srcList($str, 2, 3, 'asc', false, 'string'); $src2 = GetImgSrc::src($str, 1, 'asc', false, 'string'); var_dump($src); var_dump($src2); */ ?>