简体   繁体   English

如何使用preg_match获取数组中的字符串格式链接url .html?

[英]How to get string format link url .html in array using preg_match?

I have a sample code: 我有一个示例代码:

$array = array(
   1 => "tag/gomobi.html", 
   2 => "game.html", 
   3 => "game.php", 
   4 => "game.html", 
   5 => "game/game-mobile/feed.html"
);
foreach ($array as $url) {
   if(preg_match('/^((.*)\.html)(.*?)$/', $url, $matches)) {
       echo $matches[1].'<br />';    
   } 
}

But result can't remove value same (game.html) 但是结果不能相同地删除值(game.html)

tag/gomobi.html
game.html
game.html
game/game-mobile/feed.html

How to fix it, with result is: 如何修复它,结果是:

tag/gomobi.html
game.html
game/game-mobile/feed.html

Try: 尝试:

$array = array(
   1 => "tag/gomobi.html", 
   2 => "game.html", 
   3 => "game.php", 
   4 => "game.html", 
   5 => "game/game-mobile/feed.html"
);

print_r (array_unique($array));

Use array_unique(); 使用array_unique(); . It removes duplicate values from an array 它从数组中删除重复的值

print_r(array_unique($array));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM