简体   繁体   中英

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)

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(); . It removes duplicate values from an array

print_r(array_unique($array));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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