Php 正则表达式(preg_match_all)

Php 正则表达式(preg_match_all),php,regex,preg-match,preg-match-all,Php,Regex,Preg Match,Preg Match All,我有一个私人网站,我在那里分享视频(和一些其他东西)。 我所取得的成就是,通过preg\u match\u all()它会自动找到链接,并将带有HTML代码的视频粘贴到我的网站上 这里有一个例子: <?php $matchwith = "http://videosite.com/id1 http://videosite.com/id2 http://videosite.com/id3"; preg_match_all('/videosite\.com\/(\w+)/i', $mat

我有一个私人网站,我在那里分享视频(和一些其他东西)。 我所取得的成就是,通过
preg\u match\u all()
它会自动找到链接,并将带有HTML代码的视频粘贴到我的网站上

这里有一个例子:

    <?php
$matchwith = "http://videosite.com/id1 http://videosite.com/id2 http://videosite.com/id3";
preg_match_all('/videosite\.com\/(\w+)/i', $matchwith, $matches);  
foreach($matches[1] as $value)
{  
  print '<a href="http://videosite.com/'.$value.'">Hyperlink</a>';           
}      
    ?>
    $matchWith = "http://videosite.com/id1_movie1 http://videosite.com/id2_movie1"
               "http://videosite.com/id3_movie2 http://videosite.com/id4_movie2";
(…)之后的一切都是独一无二的

我想要的是,如果你在链接之前写第1部分和第2部分(或其他),它会自动检测到它是本视频的第1部分和第2部分

$matchwith可以包含不同的电影。

$matchwith=“第1部分http://videosite.com/id1-1 第二部分http://videosite.com/id1-2";
$matchwith = "Part 1 http://videosite.com/id1-1 Part2 http://videosite.com/id1-2";
preg_match_all('/videosite\.com\/(\w+-\d+)/i', $matchwith, $matches);  
foreach($matches[1] as $value)
{  
  print '<a href="http://videosite.com/'.$value.'">Hyperlink</a>';           
}   
preg_match_all('/videosite\.com\/(\w+-\d+)/i',$matchwith,$matchs); foreach($matches[1]为$value) { 打印“”; }
$matchwith=“第1部分http://videosite.com/id1-1 第二部分http://videosite.com/id1-2";
preg_match_all('/videosite\.com\/(\w+-\d+)/i',$matchwith,$matchs);
foreach($matches[1]为$value)
{  
打印“”;
}   

所以我相信这就是您所需要的:

<?php
$matchWith = "Movie 1 http://videosite.com/id1" . PHP_EOL .
         "Movie 1 http://videosite.com/id2" . PHP_EOL .
         "Movie 2 http://videosite.com/id3";

$arrLinks = array();
preg_match_all('%(.*)\shttp://videosite\.com/(\w+)\r{0,1}%', $matchWith, $result, PREG_SET_ORDER);
for ($matchi = 0; $matchi < count($result); $matchi++) {
    $arrLinks[$result[$matchi][1]][] = $result[$matchi][2];
}

foreach ($arrLinks as $movieName => $arrMovieIds) {
    print '<div>' . $movieName . '</div>';
    foreach ($arrMovieIds as $movieId) {
        print '<a href="http://videosite.com/'.$movieId.'">Hyperlink</a><br/>';
    }
}
?>

所以我相信这就是您所需要的:

<?php
$matchWith = "Movie 1 http://videosite.com/id1" . PHP_EOL .
         "Movie 1 http://videosite.com/id2" . PHP_EOL .
         "Movie 2 http://videosite.com/id3";

$arrLinks = array();
preg_match_all('%(.*)\shttp://videosite\.com/(\w+)\r{0,1}%', $matchWith, $result, PREG_SET_ORDER);
for ($matchi = 0; $matchi < count($result); $matchi++) {
    $arrLinks[$result[$matchi][1]][] = $result[$matchi][2];
}

foreach ($arrLinks as $movieName => $arrMovieIds) {
    print '<div>' . $movieName . '</div>';
    foreach ($arrMovieIds as $movieId) {
        print '<a href="http://videosite.com/'.$movieId.'">Hyperlink</a><br/>';
    }
}
?>



据我所知-您希望按电影的各个部分进行分组?所有部分的id都相同?不,每个部分的id都不同。例如,像youtube:每个视频都有自己的id。必须有类似的东西来识别同一部电影的各个部分。我认为通过分析ID是可能的,但是你说所有部分的ID都是不同的……如果我在链接之前写一些东西,比如第1部分=链接部分=链接。这是怎么回事?据我所知-你想要按角色分组的电影?所有角色的id都是一样的?不,每个角色的id都是不同的。例如,像youtube:每个视频都有自己的id。必须有类似的东西来识别同一部电影的各个部分。我认为通过分析ID是可能的,但是你说所有部分的ID都是不同的……如果我在链接之前写一些东西,比如第1部分=链接部分=链接。这怎么行?我得纠正你的正则表达式。我猜它使用了错误的url。也许不是?你是对的,但我仍在等待答案。我只需要两部电影就行了。谢谢。@Helena对不起,我已经更新了我的答案:)。我得到的输出是:videosite.com/id1-1“>Hyperlink videosite.com/id1-2”>Hyperlink@MozMorris-每个零件的ID都是唯一的:),即可以是xxx和YYYY是。每个零件的ID都是唯一的。可以这样做吗?电影1:第1部分=链接1第2部分=链接2电影2:第1部分=链接1第2部分=链接2?非常感谢你。我必须纠正你的正则表达式。我猜它使用了错误的url。也许不是?你是对的,但我仍在等待答案。我只需要两部电影就行了。谢谢。@Helena对不起,我已经更新了我的答案:)。我得到的输出是:videosite.com/id1-1“>Hyperlink videosite.com/id1-2”>Hyperlink@MozMorris-每个零件的ID都是唯一的:),即可以是xxx和YYYY是。每个零件的ID都是唯一的。可以这样做吗?电影1:第1部分=链接1第2部分=链接2电影2:第1部分=链接1第2部分=链接2?非常感谢,太好了。谢谢你的帮助!:)不客气:)据我所知,stackoverflow是一个在需要时提供帮助的地方。谢谢你的帮助!:)不客气:)据我所知,stackoverflow是一个在需要时提供帮助的地方。