Reputation: 8836
Hello I want to get all the unique matches of the following. How can I do it?
preg_match_all('#http://[\S]+(?:jpe?g|png|gif)#is', $content, $filtered);
$filtered = array_unique($filtered[0]);
foreach($filtered as $value){
${'varpics' . $i} .= '<img width="200px" height="200px" src="'.$value.'"><br>'; }
I still get double matches.
Upvotes: 1
Views: 1141
Reputation: 282825
No. Just pass the results through array_unique.
Unless you wanted to get real fancy with preg_replace_callback and push the matches into a set.
<?php
$content = <<<HTML
<img src="http://www.mysite.com/image1.jpg" />
<img src="http://www.mysite.com/image2.gif" />
<img src="http://www.mysite.com/image1.jpg" />
HTML;
preg_match_all('#http://[\S]+(?:jpe?g|png|gif)#is', $content, $matches);
$images = array_unique($matches[0]);
var_dump($images);
Works perfectly for me. Output:
array
0 => string 'http://www.mysite.com/image1.jpg' (length=32)
1 => string 'http://www.mysite.com/image2.gif' (length=32)
You've got a lot of other things wrong with your code though. For example, where is $i
defined? And why are you trying to define variables like that? Use an array, rather than a name appended with a number... I'm not even sure if that's valid syntax.
Upvotes: 3