php
  • regex
  • pcre
  • 2011-11-30 57 views 0 likes 
    0

    我试图在保存到数据库时替换html iframe src上的高度和宽度。我查看了preg_replace函数和PCRE表达式,但无法解决这个问题。下面列出的是我的代码和样品输入php preg_replace换出iframe的高度和宽度

    $pattern1 = '/width="[0-9]*"/'; 
    $pattern2 = '/height="[0-9]*"/'; 
    $subject = '<iframe src="http://player.vimeo.com/?title=0&amp;byline=0&amp;portrait=0" width="400" height="300" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'; 
    
    $returnValue = $preg_replace(array($pattern1, $pattern2), array('width="200"','height="200"'), $subject); 
    

    任何帮助将不胜感激!

    干杯人!

    回答

    1

    因为你在处理HTML,我会建议使用PHP的DOM功能 - http://php.net/manual/en/book.dom.php。使用HTML时,正则表达式很少是答案。

    1

    你把$放在函数的前面。

    $returnValue = preg_replace(array($pattern1, $pattern2), array('width="200"','height="200"'), $subject); 
    

    您的脚本可以被简化如下:

    $returnValue = preg_replace('/(width|height)="[0-9]*"/g', '$1="200"', $subject); 
    
    相关问题