PHP在foreach内返回preg_replace()? [英] PHP return preg_replace() inside of foreach?

查看:88
本文介绍了PHP在foreach内返回preg_replace()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿, 我不知道我在做什么错. 我有一个正则表达式模式,该模式在内部或[track = url]中查找youtube网址.

hey, i have no idea what I'm doing wrong. I have a regexp pattern that looks for a youtube url inside or [track=url].

如果正则表达式匹配,我将返回一个YouTube嵌入代码.每个视频都需要有一个唯一的ID.我正在preg_match_all foreach循环中使用一个简单的count变量创建此ID.

I'm returning a youtube embed-code if the regexp is matched. I need to have a unique ID for each video. I'm creating this ID with a simple count variable inside my preg_match_all foreach loop.

每个视频所需的$ uniqueID正常工作.如果我的$ content中有3个[track = url],我会回显3个不同的ID(玩家_1,玩家_2,玩家_3等...)

The $uniqueID that I need for each video works just fine. If I have 3 [track=url] inside my $content I get 3 different id's echoed out (player_1, player_2, player_3, etc...)

但是,我唯一的主要问题是我不知道在这种情况下如何使用preg_replace.我需要为每个视频返回embedCode,以及每个视频我要创建的唯一ID.

However ONLY MAJOR PROBLEM that I have is that I have no idea how I'm using the preg_replace in that case. I need to return the embedCode for each video with each the unique ID that I'm creating.

<?php

    $youtubeUrl = "/(\[TRACK=)((http|https)(\:\/\/)(www|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)(\.youtube\.)(com|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)([a-zA-Z0-9\-\.\/\?_=&;]*))(\])/si";

    $search = preg_match_all($youtubeUrl, $content, $matches, PREG_OFFSET_CAPTURE);
    $i = 0;

    foreach ($matches[8] as $match) {

        $watch = $match[0];

        //unique id
        $uniqueID = "player_" . $i; // player_0, player_1, player_2 ...

        //final video url
        $video = $uri . $watch;

        echo $video . "<br/>"; //correct 3 times different

        $content = preg_replace($youtubeUrl, embedCode($video, $uniqueID), $content);
        // three times player_0

        $i++;
    }

    //$content = preg_replace($youtubeUrl, embedCode($video, $uniqueID), $content);
    // three times player_3

    return $content;

?>

您知道我需要在这里做什么吗?我很无奈! 如果我在循环内调用preg_replace,我将获得player_0的embed_code的三倍,如果在foreach循环外调用它,则将获得Player_3的三倍.

Any idea what I need to do here? I'm helpless! If I call the preg_replace inside the loop I get three times the embed_code for player_0, if I call it outside the foreach loop I get three time player_3.

我在这里做错了什么!非常感谢您的帮助.

What am I doing wrong here! Thank you very much for your help.

推荐答案

您不应手动循环匹配结果,然后再运行第二个preg_replace.这是 preg_replace_callback 简化事情的理想用例:

You should not manually loop over the match results and afterwards run a second preg_replace. That's a perfect use case for preg_replace_callback to simplify things:

$i = 0;
$content = preg_replace_callback($rx_youtubeUrl, "cb3", $content);

function cb3 ($match) {

    $watch = $match[8];
    global $i, $uri;
    $i++;

    //unique id
    $uniqueID = "player_" . $i; // player_0, player_1, player_2 ...

    //final video url
    $video = $uri . $watch;

    return embedCode($video, $uniqueID);
}

对于$ uniqueID,您可能必须使用全局或静态变量.

For the $uniqueID you might have to use a global or static variable.

相同的ID出现3次是由于preg_replace运行了所有正则表达式.它不仅找到当前的[TRACK=..,而且一次剥离所有内容.您可能还可以使用静态的str_replace.

That the same ID appeared three times is caused by the preg_replace running over all occurrences of the regex. It doesn't just find the current [TRACK=.., but strips all at once. You could have used a static str_replace alternatively.

这篇关于PHP在foreach内返回preg_replace()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆