需要有关 php 中简单的 preg_match/preg_replace 正则表达式的帮助

发布于 2024-08-05 18:48:18 字数 897 浏览 4 评论 0原文

我正在尝试执行一项简单的任务,即使用共同的宽度/高度更改数千个音乐视频嵌入代码。

例如,我有以下代码:

<object width="480px" height="407px" >
    <param name="allowFullScreen" value="true"/>
    <param name="wmode" value="transparent"/>
    <param name="movie" value="http://mediaservices.myspace.com/services/media/embed.aspx/m=1311720,t=1,mt=video"/>
    <embed src="http://mediaservices.myspace.com/services/media/embed.aspx/m=1311720,t=1,mt=video"
        width="480" height="407" allowFullScreen="true"
        type="application/x-shockwave-flash"
        wmode="transparent">
    </embed>
</object>

仅为了易读性而添加换行符

我需要编辑 标签,一个具有“px”后缀,另一个根本没有(这是完全随机的,有些代码在所有情况下都有,有些则没有) 。

首先,我试图找出现有视频的宽度/高度...找到宽高比...然后用新值替换现有值(宽度=“640”和高度=“xxx”这是基于视频的宽高比)。

I'm trying to do a simple task of altering thousands of music video embed codes with a common with/height.

For example, I have the following code:

<object width="480px" height="407px" >
    <param name="allowFullScreen" value="true"/>
    <param name="wmode" value="transparent"/>
    <param name="movie" value="http://mediaservices.myspace.com/services/media/embed.aspx/m=1311720,t=1,mt=video"/>
    <embed src="http://mediaservices.myspace.com/services/media/embed.aspx/m=1311720,t=1,mt=video"
        width="480" height="407" allowFullScreen="true"
        type="application/x-shockwave-flash"
        wmode="transparent">
    </embed>
</object>

Line breaks added only for legibility

I need to edit the width/height parameters in both the <object> and <embed> tags, one having a 'px' suffix, and the other not having one at all (which is totally random, some codes have it in all cases, others do not).

Firstly, I'm trying to find out the width/height of the existing video.... finding the aspect ratio... and then replacing the existing values with the new values (width = "640" and height="xxx" which is based on the aspect ratio of the video).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

脱离于你 2024-08-12 18:48:18

以下是获取宽度和高度的方法

preg_match('/width="(\d+)(px)?" height="(\d+)(px)?"/', $text, $matches);

$width = intval($matches[1]);
$height = intval($matches[3]);

计算新的高度,如下所示:

$new_width = 640;
$new_height = intval($new_width * $height / $width);

并像这样替换:

$text = preg_replace('/width="(\d+)(px)?" height="(\d+)(px)?"/',
                     'width="' . $new_width . '" height="' . $new_height . '"',
                      $text);

Here's how to get the width and height

preg_match('/width="(\d+)(px)?" height="(\d+)(px)?"/', $text, $matches);

$width = intval($matches[1]);
$height = intval($matches[3]);

Calculate the new height like this:

$new_width = 640;
$new_height = intval($new_width * $height / $width);

And replace like so:

$text = preg_replace('/width="(\d+)(px)?" height="(\d+)(px)?"/',
                     'width="' . $new_width . '" height="' . $new_height . '"',
                      $text);
小霸王臭丫头 2024-08-12 18:48:18
$embed_clean = preg_replace('/width=([^"]"\d+(%|px|)[^"]")/','width="'.$CONFIG.'"',$embed_clean);
$embed_clean = preg_replace('/width=([^"]"\d+(%|px|)[^"]")/','width="'.$CONFIG.'"',$embed_clean);
清浅ˋ旧时光 2024-08-12 18:48:18

使用 SimpleHTMLDOM

require_once("simplehtmldom.php");

$dom = str_get_html($text);
foreach ($dom->find('object') as $obj) {
    $width = intval($obj->width);
    $height = intval($obj->height);
    $height = intval(640 * $height / $width);
    $obj->width = 640;
    $obj->height = $height;
    $embed = $obj->find('embed',0);
    if ($embed != null) {
        $embed->width = 640;
        $embed->height = $height;
    }
}
$text = $dom->save();

Using SimpleHTMLDOM:

require_once("simplehtmldom.php");

$dom = str_get_html($text);
foreach ($dom->find('object') as $obj) {
    $width = intval($obj->width);
    $height = intval($obj->height);
    $height = intval(640 * $height / $width);
    $obj->width = 640;
    $obj->height = $height;
    $embed = $obj->find('embed',0);
    if ($embed != null) {
        $embed->width = 640;
        $embed->height = $height;
    }
}
$text = $dom->save();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文