Home  >  Article  >  Backend Development  >  A simple example of extracting any attribute in the image img tag with PHP_PHP Tutorial

A simple example of extracting any attribute in the image img tag with PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:18:06797browse

Copy code The code is as follows:

/* PHP regular extraction of any attributes in the image img tag */

$str = '


PHP regular extraction or change of images Any attribute in the img tag
';

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

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

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

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

//1. Replace src="/uploads/images/20100516000.jpg" with 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/621692.htmlTechArticleCopy the code as follows: ?php /* PHP regular extraction of any attribute in the image img tag*/ $str = 'centerimg src="/uploads/images/20100516000.jpg" height="120" width="120"br /PHP regular...
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