需要一个正则表达式来获取php中图像url的宽度和高度

问题描述:

My image class

$temp=<img class="alignnone wp-image-6" alt="2166105529_70dd50ef4b_n" src="http://192.168.1.12/wordpress/wp-content/uploads/2013/03/2166105529_70dd50ef4b_n-300x175.jpg" width="180" height="105">

I want to grab width and height from the url i.e. width="180" and height="105"

I had already got the src part of that using

preg_replace('/<img\s.*?\bsrc="(.*?)".*?>/si', $temp, $matches);

$matches= it contains extracted src like this

http://192.168.1.12/wordpress/wp-content/uploads/2013/03/2166105529_70dd50ef4b_n-300x175.jpg

Now how to extract width and height using regex or any other method also accepted??

我的图像类 p>

$ temp = &lt; img class =“alignnone wp-image-6”alt =“2166105529_70dd50ef4b_n”src =“http://192.168.1.12/wordpress/wp-content/uploads/2013/03/2166105529_70dd50ef4b_n-300x175.jpg”width =“180”height = “105”&gt; code> p>

我想从网址中获取宽度和高度,即 width =“180” code>和 height =“ 105“ code> p>

我已经使用 p>

preg_replace('/&lt; img \ s)得到了src的一部分 。*?\ bsrc =“(。*?)”。*?&gt; / si',$ temp,$ matches); code> p>

$ matches =它包含已提取 像这样的src p>

  http://192.168.1.12/wordpress/wp-content/uploads/2013/03/2166105529_70dd50ef4b_n-300x175.jpg
  code>   pre> 
 
 

现在如何使用正则表达式或任何其他方法提取宽度和高度?? p> div>

Using the dom class in php is a better way. Much easier to use. Example: http://sandbox.onlinephpfunctions.com/code/c6d89fc6e0803ac38a3bc1ea9c61e081c1b71f08

$dom = new DOMDocument();
$dom->loadHTML('<img class="alignnone wp-image-6" alt="2166105529_70dd50ef4b_n" src="http://192.168.1.12/wordpress/wp-content/uploads/2013/03/2166105529_70dd50ef4b_n-300x175.jpg" width="180" height="105">');

$img = $dom->getElementsByTagName('img');

$src= $img->item(0)->getAttribute('src'); 
$width= $img->item(0)->getAttribute('width'); 
$height= $img->item(0)->getAttribute('height'); 

echo $src ."<br/>";
echo $width."<br/>";;
echo $height."<br/>";;

You can just do

list($height, $width) = explode("x",substr(strrchr( $url , "-" ),1,-4));

And if I misunderstood and you need to get it from the attributes, not the actual url of the image, then

$url= '<img class="alignnone wp-image-6" alt="2166105529_70dd50ef4b_n" src="http://192.168.1.12/wordpress/wp-content/uploads/2013/03/2166105529_70dd50ef4b_n-300x175.jpg" width="180" height="105">';

echo substr(strstr( $url , "width" ),0,-1);

Will echo

width="180" height="105"

I prefer to use PHP's DOM extension for this, because it's more reliable and knows how to parse HTML correctly, and knows something about character sets.

<?php

$temp='<img class="alignnone wp-image-6" alt="2166105529_70dd50ef4b_n" src="http://192.168.1.12/wordpress/wp-content/uploads/2013/03/2166105529_70dd50ef4b_n-300x175.jpg" width="180" height="105">';

$dom = new \DomDocument;
$dom->loadHTML($temp);

$img = $dom->getElementsByTagName('img')->item(0);

// Note: Values are returned as strings, not as numbers
$src = $img->getAttribute('src');
preg_match('/(.+)-([0-9]+)x([0-9]+)\.jpg$/', $src, $matches);

$width = $matches[2];
$height = $matches[3];