如果您想从多行文本中获取以开头的行api_images
并忽略所有其他行,则可以使用它。
流动:
- 获取从开始
api_images
到行尾的所有行。
- 删除不需要的字符。
- 在 处拆分字符串
,
。
- 根据需要处理。
代码:
<?php
$str = "api_images = ['/files/a.jpg','/files/b.jpg','/files/c.jpg'];
api_titles = 3;
api_descriptions = 42;
api_images = ['/files/1.jpg','/files/2.jpg','/files/3.jpg'];
api_titles = 3;
api_descriptions = 42;";
//Find all the lines starting with "api_images"
preg_match_all("/(api_images.*)/", $str, $matches);
$api_images = $matches[0];
$count_api_images = count($api_images);
for($i=0;$i<$count_api_images;$i++){
$api_images[$i] = str_replace("api_images = [", "", $api_images[$i]);
$api_images[$i] = str_replace("'", "", $api_images[$i]);
$api_images[$i] = str_replace("]", "", $api_images[$i]);
$api_images[$i] = str_replace(";", "", $api_images[$i]);
$api_images[$i] = explode(",", $api_images[$i]);
}
echo "<pre>";
print_r($api_images);
echo "</pre>";
?>
每个字符串 ie等都可以通过/files/a.jpg
,等访问。/files/avd.jpg
/files/5.jpg
$api_images[0][0]
$api_images[0][1]
$api_images[0][2]
现场演示