尝试用 PHP 替换字符串($content)中的 height="" 和 width="" 值,我尝试 preg replace 无济于事,并建议我做错了什么?
示例内容为:
$content = '<iframe width="560" height="315" src="http://www.youtube.com/embed/c0sL6_DNAy0" frameborder="0" allowfullscreen></iframe>';
下面的代码:
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);
}
}
在这里我只是参加比赛并搜索我的高度和宽度
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 $new;
}
$new = $match;
return $new;
}
可能有更短的方法可以做到这一点,但是,我真的在 6 个月前才开始编程。
提前致谢!