Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3599276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:20:52+00:00 2026-05-18T20:20:52+00:00

I have a regex pattern that searches for words in a text file. How

  • 0

I have a regex pattern that searches for words in a text file. How do I ignore duplicates?

For instance, take a look at this code

$pattern = '/(lorem|ipsum|daboom|pahwal|ababaga)/i';
$num_found = preg_match_all( $pattern, $string, $matches );

echo "$num_found match(es) found!";
echo "Matched words: " . implode( ',', $matches[0] );

If I have more than one say lorem in the article, the output will be something like this

5 matches found!
Matched words: daboom,lorem,lorem,lorem,lorem

I want the pattern to only find the first occurrence, and ignore the rest, so the output should be:

2 matches found!
Matched words: daboom,lorem
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:20:53+00:00Added an answer on May 18, 2026 at 8:20 pm

    Do an array_unique on $matches[0]. And maybe an array_map with strtolower if you want the unique to be case insensitive.

    $pattern = '/(lorem|ipsum|daboom|pahwal|ababaga)/i';
    preg_match_all( $pattern, $string, $matches );
    $matches = $matches[0]?array_unique(array_map('strtolower', $matches[0])):array();
    
    echo count($matches)." match(es) found!";
    echo "Matched words: " . implode( ',', $matches );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python compiled regex patterns have a findall method that does the following: Return
i have a list of regex patterns (stored in a list type) that I
I have a regex that is going to end up being a bit long
I have a regex call that I need help with. I haven't posted my
I have this regex I built and tested in regex buddy. _ [ 0-9]{10}+
I have a RegEx that is working for me but I don't know WHY
I have simple regex \.*\ for me its says select everything between and ,
I have a regex created from a list in a database to match names
Anyone have a regex to strip lat/long from a string? such as: ID: 39.825
A tutorial I have on Regex in python explains how to use the re

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.