要从HTML内容或PHP字符串/变量中获取所有图像URL,并按特定扩展对其进行过滤,并且仅包括远程域,请使用以下代码:
$data = \'alsdj<img src="http://localdomain.com/donotinclude.jpg">fklasjdf<img src="image.jpg">asdfasdf<img src="http://remotedomain.com/image.jpg">asdfasdfsf<img src="dont_include_me.png">asdfasfasdf\';
$images = array();
// Domains to not include images from (for instances where the full URL is specified to image)
$localhosts = array( \'localdomain.com\' );
// Remove or add any extensions TO be included
$allowed_extensions = array( \'jpg\', \'jpeg\' );
preg_match_all(\'/(img|src)\\=(\\"|\\\')[^\\"\\\'\\>]+/i\', $data, $media);
unset($data);
$data=preg_replace(\'/(img|src)(\\"|\\\'|\\=\\"|\\=\\\')(.*)/i\',"$3",$media[0]);
foreach($data as $url)
{
$url_data = parse_url( $url );
$info = pathinfo($url);
// Goto next, we\'re only looking for remote hosts
if( ! array_key_exists( \'host\', $url_data ) ) continue;
// Check if this is an extension we want to include
if( array_key_exists( \'extension\', $info ) && in_array( $info[\'extension\'], $allowed_extensions) ){
// Verify this isn\'t one of our local hosts (where the full URL is specified)
if( ! in_array( $url_data[\'host\'], $localhosts ) ){
array_push($images, $url);
}
}
}
echo \'Total Images: \' . count( $images );
// This is just linebreaks for display formatting
print "\\n\\n";
echo \'Image Data:\';
var_dump( $images );
代码示例:
https://glot.io/snippets/ekahzvu8sh上述代码示例的结果输出:
Total Images: 1
Image Data:array(1) {
[0]=>
string(27) "http://remotedomain.com/image.jpg"
}
确保在
$localhosts
数组,这样,如果出于某种原因指定了完整的URL,它将不包括该URL。
使用上面的示例代码,下面是更新后的函数,用于使用该代码并返回图像数:
function post_photo_count_xpath( $post_id ) {
$post = get_post( $post_id );
if( ! $post ) return 0;
$data = $post->post_content;
$images = array();
// Domains to not include images from (for instances where the full URL is specified to image)
$localhosts = array( \'localdomain.com\' );
// Remove or add any extensions TO be included
$allowed_extensions = array( \'jpg\', \'jpeg\' );
preg_match_all(\'/(img|src)\\=(\\"|\\\')[^\\"\\\'\\>]+/i\', $data, $media);
unset($data);
$data=preg_replace(\'/(img|src)(\\"|\\\'|\\=\\"|\\=\\\')(.*)/i\',"$3",$media[0]);
if( empty( $data ) ) return 0;
foreach( $data as $url ){
$url_data = parse_url( $url );
$info = pathinfo($url);
// Goto next, we\'re only looking for remote hosts
if( ! array_key_exists( \'host\', $url_data ) ) continue;
// Check if this is an extension we want to include
if( array_key_exists( \'extension\', $info ) && in_array( $info[\'extension\'], $allowed_extensions) ){
// Verify this isn\'t one of our local hosts (where the full URL is specified)
if( ! in_array( $url_data[\'host\'], $localhosts ) ){
array_push($images, $url);
}
}
}
return count( $images );
}
资源:
https://davebrooks.wordpress.com/2009/04/22/php-preg_replace-some-useful-regular-expressions/