尝试用PHP替换字符串($ content)中的height =""和width =""值,我试过preg替换无效,并建议我做错了什么?
示例内容如下:
$content = '<iframe width="560" height="315" src="http://www.youtube.com/embed/c0sL6_DNAy0" frameborder="0" allowfullscreen></iframe>';
Run Code Online (Sandbox Code Playgroud)
代码如下:
if($type === 'video'){
$s = $content;
preg_match_all('~(?|"([^"]+)"|(\S+))~', $s, $matches);
foreach($matches[1] as $match){
$newVal = $this->_parseIt($match);
preg_replace($match, $newVal, $s);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我只需要比赛并搜索我的身高和宽度
function _parseIt($match)
{
$height = "height";
$width = "width";
if(substr($match, 0, 5) === $height){
$pieces = explode("=", $match);
$pieces[1] = "\"175\"";
$new = implode("=", $pieces);
return $new;
}
if(substr($match, 0, 5) === $width){
$pieces = explode("=", $match);
$pieces[1] = "\"285\"";
$new = implode("=", $pieces);
return …Run Code Online (Sandbox Code Playgroud)