width="200" height="300"
去这个用PHP
width:200px; height:300px;
基本上我想删除引号和等号并在每个属性后插入一个分号
width="200" height="300"
去这个用PHP
width:200px; height:300px;
基本上我想删除引号和等号并在每个属性后插入一个分号
您可以使用正则表达式:
<?php
$html = 'width="200" height="300"';
$pattern = '/width="(\d+)"\s+height="(\d+)"/';
preg_match($pattern, $html, $matches);
$style = "width:".$matches[1]."px; height:".$matches[2]."px;";
echo $style;
?>
您可以使用str_replace
或regu年轻的表达。
至于具体的例子:
$string = 'width="200" height="300"'
$string = str_replace('="', ':', $string);
$string = str_replace('"', 'px;', $string);
但要注意,如果还有其他属性或某事物否则 - 正则表达式可能是更好的方法。
只是,如果你不打算直接包括它不使用的getimagesize()
是输出格式。
使用前两个值:
list($width, $height) = getimagesize("img/flag.jpg");
echo 'width:'.$width.'px; height:'.$height.'px;';
大概从HTML移动属性的内嵌样式属性? – 2012-01-08 20:06:47
@cillosis getimagesize(x)给出了html的宽度和高度attr。我想将其转换为嵌入式css – Pinkie 2012-01-08 20:11:25
如果不是特殊情况,请不要将其替换为图像。这是有道理的(从内联样式到'width'和'height'属性)。请参阅:http://stackoverflow.com/questions/1247685/should-i-specify-height-and-width-attributes-for-my-imgs-in-html – pawel 2012-01-08 20:45:26