1

I want to remove duplicates image. How to use array_unique? This is what I have tried:

$text = 'text image.jpg flower.jpg txt image.jpg';
$pattern = '/[\w\-]+\.(jpg|png|gif|jpeg)/';
$result = preg_match_all($pattern, $text, $matches);
$matches = $matches[0];

foreach ($matches as $klucz => $image) { 
    echo $image;
}
Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
harry
  • 33
  • 5

3 Answers3

1

array_unique should be applied to an array. So split your string into chunks and use it:

$names = array_unique(explode(' ', $text));
VisioN
  • 143,310
  • 32
  • 282
  • 281
1

First, explode() the string around " " then call array_unique(). Eg below:

$text = 'text image.jpg flower.jpg txt image.jpg';
$arr = explode(" ", $text);
$arr = array_unique($arr);
print_r($arr); // Array ( [0] => text [1] => image.jpg [2] => flower.jpg [3] => txt )

Read more:

Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
0

I used preg_match because in the text the pictures are from the path

$text = 'text image.jpg flower.jpg txt <img src="path/image.jpg" '>;
harry
  • 33
  • 5
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/18833881) – Bryce Siedschlaw Feb 15 '18 at 18:14
  • @BryceSiedschlaw: this is the OP's Answer to his own question - your review is inappropriate. – Cindy Meister Feb 15 '18 at 18:29
  • 1
    @CindyMeister It did not appear to be an answer. If it's an actual answer to their question, they should edit it with working code and useful description. Currently, it appears to be more like a comment. – Bryce Siedschlaw Feb 15 '18 at 18:58