小编mor*_*t25的帖子

试图在PHP中替换高度和宽度值

尝试用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)

php regex preg-replace

2
推荐指数
1
解决办法
4870
查看次数

标签 统计

php ×1

preg-replace ×1

regex ×1