的屬性寬度如何從這個如何改變IMG風格寬度在PHP
<img style="width:200px; height:300px; ...
去這個
<img width="200" height="300" ...
與PHP?
的屬性寬度如何從這個如何改變IMG風格寬度在PHP
<img style="width:200px; height:300px; ...
去這個
<img width="200" height="300" ...
與PHP?
這是會使用一些正則表達式:
//use SimpleXMLElement to parse the attributes
$img = new SimpleXMLElement($imgStr);
$pattern = '~([a-z-]+)\s*:\s*([^;$]+)~si';
//convert the value of the style attribute into an associative array
if (preg_match_all($pattern, $img['style'], $match)) {
$style[] = array_combine(
array_map('strtolower', $match[1]),
array_map(function($val) { return trim($val, '"\' '); }, $match[2])
);
}
// add the width and height attributes and get the new html
$img->addAttribute('width', $style[0]['width']);
$img->addAttribute('height', $style[0]['height']);
$newImgStr = $img->asXML();
// remove the width and height rules from the style attribute
$newImgStr = preg_replace('/width:([^;$]+);/', '', $newImgStr);
$newImgStr = preg_replace('/height:([^;$]+);/', '', $newImgStr);
一個簡單的方法可以不觸及到HTML ...
找到一種方法來定位在你的CSS這些圖像和剛剛適應顯示器
div.myimages img {
max-with:320px;
height: auto!important;
}
我知道這不是最「強大」和優雅的解決方案,但它很簡單!我不明白爲什麼它值得-1! 這個問題本身就不夠優雅! Tristup的解決方案遠比我的好,我同意並且遺憾的是沒有這個想法......但是+1對於所有複雜的正則表達式和-1對於簡單的舔 - 它堅持 - 它......悲傷) – 2014-11-24 21:31:55
如果樣式已經加載與IMG則solutiona可以使用jQuery進行:
$(function() {
width=$(this).css("width");
height=$(this).css("height");
$(this).attr("width",width);
$(this).attr("height",height);
});
希望它能滿足您的需求。
爲什麼你需要這個? – 2014-11-24 04:16:08
第一個比第二個好。你爲什麼想去? – Pupil 2014-11-24 04:17:20
只需在圖像寬度大於320的情況下,我只需要在第三方應用程序接收的HTML代碼中重新調整圖像的大小。寬度在每個「img」標記的「樣式」屬性中定義。 – 2014-11-24 04:19:49