Home  >  Article  >  Backend Development  >  Parse PHP regular expression to extract or replace img tag attribute_PHP tutorial

Parse PHP regular expression to extract or replace img tag attribute_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:02:56940browse

/*PHP regular extraction of any attributes in the image img tag*/
$str = '


PHP regular extraction or change of any attribute in the image img tag
';

//1. Get the entire image code
preg_match('/]*?srcs*=s*('|")(.*? )\1[^>]*?/?s*>/i',$str,$match);
echo $match[0];

//2. Get width
preg_match('//i',$str,$match );
echo $match[1];

//3. Get height
preg_match('//i',$str,$match );
echo $match[1];

//4. Get src
preg_match('//i',$str,$match);
echo $match[1];

/*PHP regular replacement of any attribute in the image img tag*/
//1. Replace src="/uploads/images/20100516000.jpg" for src="/uploads/uc/images/20100516000.jpg")
print preg_replace('/()/i',"${1}uc/images/${3}",$str);
echo "


";

//2. Replace src="/uploads/images/20100516000.jpg" with src="/uploads/uc/images/20100516000.jpg", and omit the width and height
print preg_replace(' /(/i',"${1} $ {2}uc/images/${3}>",$str);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327894.htmlTechArticle?php /*PHP regular extraction of any attributes in the image img tag*/ $str = 'centerimg src=" /uploads/images/20100516000.jpg" height="120" width="120"br /PHP regular extraction or change of image img tag...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn