If from many lines of text, you want to fetch the line starting with api_images
and ignore all the other lines, you can use this.
Flow:
- Fetch all the lines starting with
api_images
till the line-ending.
- Remove unwanted characters.
- Split the string at
,
.
- Process as desired.
Code:
<?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>";
?>
Each string, i.e./files/a.jpg
, /files/avd.jpg
/files/5.jpg
etc can be accessed by $api_images[0][0]
, $api_images[0][1]
, $api_images[0][2]
and so on.
Live demo