没有不值得去解决的问题,也没有不值得去学习的技术!

How to Check if a Variable Contains a URI in PHP 7.4

I use the parse_url function to get the scheme and check whether it is http or https. The final filtered results are as follows, which meet expectations

1. Now I need to extract data in URI format from a one-dimensional array. The printout of the one-dimensional array is shown in Figure 1.

Now I need to extract data in URI format from a one-dimensional array. The printout of the one-dimensional array
Figure 1
Plaintext

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 based on filter_var is as follows, and the printout is as follows. It does not meet expectations because it includes: internal://policy_pages/29

PHP

foreach ($images->all() as $image) {
	var_dump($image, filter_var($image, FILTER_VALIDATE_URL));
	echo "\n";
}

Plaintext

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. I use the parse_url function to get the scheme and check whether it is http or https. The final filtered results are as follows, which meet expectations. As shown in Figure 2

I use the parse_url function to get the scheme and check whether it is http or https. The final filtered results are as follows, which meet expectations
Figure 2
PHP

$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);

Plaintext

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"
  }
}


关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系