In PHP 7.4, determine whether the variable value is data in URI format
1. Now you need to take the URI format data from a one-dimensional array. The print result of a one-dimensional array, as shown in Figure 1
array(134) {
[14]=>
bool(false)
[15]=>
string(0) ""
[16]=>
string(6) "center"
[17]=>
string(7) "#ffffff"
[18]=>
string(95) "https://xxx.s3.us-east-2.amazonaws.com/image/736589fac73c7dfdbf61efadb49a95fb9af16efb.jpeg"
[19]=>
string(30) "Image with text overlay"
[20]=>
string(8) "Shop Now"
}
2. The implementation is as follows based on the filter_var code, and the printing result is as follows, which does not meet the expectations, which exists: internal://policy_pages/29
foreach ($images->all() as $image) {
var_dump($image, filter_var($image, FILTER_VALIDATE_URL));
echo "\n";
}
string(11) "collections"
bool(false)
bool(false)
bool(false)
string(6) "center"
bool(false)
string(4) "grid"
bool(false)
string(18) "this is collection"
bool(false)
string(19) "main-index-carousel"
bool(false)
string(5) "slide"
bool(false)
bool(false)
bool(false)
string(0) ""
bool(false)
string(6) "center"
bool(false)
string(7) "#ffffff"
bool(false)
string(95) "https://xxx.us-east-2.amazonaws.com/image/736589fac73c7dfdbf61efadb49a95fb9af16efb.jpeg"
string(95) "https://xxx.us-east-2.amazonaws.com/image/736589fac73c7dfdbf61efadb49a95fb9af16efb.jpeg"
string(30) "Image with text overlay"
bool(false)
string(8) "Shop Now"
bool(false)
string(26) "internal://policy_pages/29"
string(26) "internal://policy_pages/29"
3. Obtain the scheme through the function parse_url to determine whether it is http or https. The final filtering result is as follows, in line with expectations. as shown in Figure 2
$images = collect($data['sections'])->flatten()->reject(function ($image) {
return !filter_var($image, FILTER_VALIDATE_URL);
})->reject(function ($image) {
return !in_array(parse_url($image, PHP_URL_SCHEME), ['http', 'https']);
});
var_dump($images);
object(Illuminate\Support\Collection)#4918 (1) {
["items":protected]=>
array(4) {
[10]=>
string(107) "https://cdn.xxx.com/image/2022/04/fa5d91ba9fd60ef1b050560895d2f15c02852f0d9669654de4c01c8b404af3f3.jpeg"
[24]=>
string(95) "https://xxx.us-east-2.amazonaws.com/image/736589fac73c7dfdbf61efadb49a95fb9af16efb.jpeg"
[41]=>
string(95) "https://xxx.us-east-2.amazonaws.com/image/7a204816a9625475044c8196bfb154b39aa3f12b.jpeg"
[129]=>
string(95) "https://xxx.us-east-2.amazonaws.com/image/736589fac73c7dfdbf61efadb49a95fb9af16efb.jpeg"
}
}

