2010-08-09 34 views
1

我有人插入视频代码以显示在我的网站上。我想确保这些视频尺寸不超过590像素的宽度。使用PHP获取某些<object> HTML代码的尺寸

因此,例如它插入一个640 x 385的视频,我的脚本会找出一些新的尺寸,这样它的宽度只有590像素(例如:590 x 366)......我已经掌握了所有的数学和比例的东西计算出来,它只是从高度和宽度中提取高度和宽度,然后用新尺寸替换它们。

下面是我需要获取维度的一些代码示例。还有下面的代码,但一旦我弄清楚如何制定出我的尺寸可以直接复制代码,

<object width="640" height="385"> 
    <param name="movie" value="http://www.youtube.com/v/ik6N1TLL2TI&hl=en_US&fs=1"></param> 
    <param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param> 
    <embed src="http://www.youtube.com/v/ik6N1TLL2TI&hl=en_US&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed> 
</object> 

我有一些代码,我在过去与帮助它增加了一个网址在他们面前没有http://的链接。我试图玩代码,但无法弄清楚。如果有帮助,这里是原始代码:

$content_text = preg_replace('% 
    (<a\b[^>]*?href\s*+=\s*+) # capture first chunk of A tag in group 1 
    ("|\'|)         # capture HREF attribute delimiter in group 2 
    (?!\w++://|/)      # ensure URL has no scheme nor abs path 
    ([^"\' >]++)       # capture relative URI path in group 3 
    \2          # match closing delimiter for HREF attribute 
    %ix', '$1"' . SITEURL . '/$3"', $content_text); 

感谢您的帮助提前!

+0

您应该阅读此:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#answer-1732454 – zneak 2010-08-09 04:19:34

回答

2

可能会通过煤炭为此倾斜,因为您应该在PHP中使用HTML DOM parser。但这里是正则表达式作为“替代”方法。

preg_match('~<object.*(width|height)=(.*) (width|height)=(.*)(>|\s)~isU', $contents, $matches); 
if (!empty($matches)) { 
    $dimensions[strtolower($matches[1])] = str_replace(array("'", '"'), '', $matches[2]); 
    $dimensions[strtolower($matches[3])] = str_replace(array("'", '"'), '', $matches[4]); 
} 

HTML Parser可能是一个明智的选择,但上面应该抓住的宽度和高度,无论引号或顺序,将它们放置在$dimensions['width']$dimensions['height']

就像我说的,这只是一个替代的例子,HTML Parser可能是更明智的路要走。也正则表达式可能会写得更好,但哦。希望能帮助到你。

+0

谢谢Premiso,HTML解析器是去的方式:)欣赏你的帮助! – 2010-08-09 23:26:26