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 8279927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:29:18+00:00 2026-06-08T09:29:18+00:00

I have a string of text, which I then grab a URL from with

  • 0

I have a string of text, which I then grab a URL from with php regex. There can be any number of links, so I’m using

 preg_match_all

The problem is that for some reason when I put in one link, it’s thinking that there are 3. When I do array unique it filters out the middle value, but not the last one.

Here is the code below

 $bodyMessage = imap_body($hMail,$idxMsg);
 $bodyMessage = quoted_printable_decode($bodyMessage);

 preg_match_all('((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)', $bodyMessage, $matches, PREG_PATTERN_ORDER);
 $links = array_unique($matches[0]);
 print_r($links); 

The output of print_r($links) is:

 Array ( [0] => http://usnews.msnbc.msn.com/_news/2012/07/20/12861792-6-year-old-girl-confirmed-to-have-been-killed-in-colorado-theater-shootings?lite 
 [2] => http://usnews.msnbc.msn.com/_news/2012/07/20/12861792-6-year-old-girl-confirmed-to-have-been-killed-in-colorado-theater-shootings?lite

The body of the email that it parses is:

 --20cf300e4d7d02c34004c55e1489 Content-Type: text/plain; charset=ISO-8859-1 @bill http://usnews.msnbc.msn.com/_news/2012/07/20/12861792-6-year-old-girl-confirmed-to-have-been-killed-in-colorado-theater-shootings?lite --20cf300e4d7d02c34004c55e1489 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable @bill 

Any ideas?
Thanks!

Edit:

I followed the suggestion, by trimming, and that returns an empty array

 function trims($l){
                    trim($l);   
                }
                $links = $matches[0];
                $trimmedLinks = array_map("trims", $links);
                $trimmedLinks = array_unique($trimmedLinks);
                print_r($trimmedLinks); // = Array ( [0] => ) 

EDIT:

I think this might have something to do with grabbing the body message from imap. When i copy and paste the the string of text from imap, and set that = to $bodyMessage, then it works…
Suggestions?

  • 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-06-08T09:29:20+00:00Added an answer on June 8, 2026 at 9:29 am

    You should have pattern like this

    ((?:https?|ftp|gopher|telnet|file|notes|ms-help):(?:(?://)|(?:\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)
    

    with non-capturing groups. If you put ?: in bracket, you’ll get non-capturing group. And then an array will be:

    Array ( [0] => http://usnews.msnbc.msn.com/_news/2012/07/20/12861792-6-year-old-girl-confirmed-to-have-been-killed-in-colorado-theater-shootings?lite )
    

    Edit: The answer to this problem is to use imap_fetchbody instead

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string headerText, which containing text ABC (2-8 Days). How can truncate
I have a requirement. I have a text which can contain any characters. a)
I have a string which contains binary data ( non-text data ). How do
I am trying to grab a number from a text box entry, convert it
I have a Regex in C# to detect emails in text and then I
i have made a function taketext() which returns a string value of entered text
I am reading in a String from a text file which contains a date
I am extracting some text from html which is passed as a string. The
I have the following very simple regex, which matches HTML tags in a string.
I have code which uses the StreamReader to read HTML from a file, then

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.