php正则提取图片img标记中的任意属性,PHP正则提取图片img标记中的任意属性

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:58   2153   0

/*PHP正则提取图片img标记中的任意属性*/

$str = '

20100516000.jpg
PHP正则提取或更改图片img标记中的任意属性
';

//1、取整个图片代码

preg_match('/]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i',$str,$match);

echo $match[0];

//2、取width

preg_match('//i',$str,$match);

echo $match[1];

//3、取height

preg_match('//i',$str,$match);

echo $match[1];

//4、取src

preg_match('//i',$str,$match);

echo $match[1];

/*PHP正则替换图片img标记中的任意属性*/

//1、将src="/uploads/images/20100516000.jpg"替换为src="/uploads/uc/images/20100516000.jpg")

print preg_replace('/()/i',"\${1}uc/images/\${3}",$str);

echo "


";

//2、将src="/uploads/images/20100516000.jpg"替换为src="/uploads/uc/images/20100516000.jpg",并省去宽和高

print preg_replace('/(/i',"\${1} \${2}uc/images/\${3}>",$str);

/*PHP正则提取图片img标记中的src属性*/

$str = '

animals.png
PHP正则提取或更改图片img标记中的任意属性
';

//4、取src

preg_match('//i',$str,$match);

$src = $match[1];

$filename_index = strripos($src, '/');

$filename = substr($src,$filename_index+1);

$host_index = stripos($src,'//img.w3cschool.cn');

if ($host_index === 0) {

$imgdir = substr($src, 18, $filename_index-18);

} else if($host_index === FALSE){

$imgdir = substr($src, 0, $filename_index);

}

print_r($imgdir);

$src_arr = explode('/', $src);

print_r($src_arr);

echo $filename;

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP