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

  • Home
  • SEARCH
  • 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 6720181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:10:55+00:00 2026-05-26T09:10:55+00:00

I have a string that contain links. I want my php to do different

  • 0

I have a string that contain links. I want my php to do different things with my links, depending on the url.

Answer:

function fixLinks($text)
{       
        $links = array();
        $text = strip_tags($text);    
        $pattern = '!(https?://[^\s]+)!'; 
        if (preg_match_all($pattern, $text, $matches)) {
            list(, $links) = ($matches);
        }

        $i = 0;
        $links2 = array();
        foreach($links AS $link) {
            if(strpos($link,'youtube.com') !== false) {
                $search = "!(http://.*youtube\.com.*v=)?([a-zA-Z0-9_-]{11})(&.*)?!";
                $youtube = '<a href="youtube.php?id=\\2" class="fancy">http://www.youtube.com/watch?v=\\2</a>';
                $link2 = preg_replace($search, $youtube, $link);
            } else {
                $link2 = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\-\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1" target="_blank"><u>$1</u></a>', $link);
            }
            $links2[$i] = $link2;
            $i++;     
        }
        $text = str_replace($links, $links2, $text);
        $text = nl2br($text);

    return $text;
}
  • 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-26T09:10:56+00:00Added an answer on May 26, 2026 at 9:10 am

    First of all, ditch eregi. It’s deprecated and will disappear soon.

    Then, doing this in just one pass is maybe a stretch too far. I think you’ll be better off splitting this into three phases.

    Phase 1 runs a regex search over your input, finding everything that looks like a link, and storing it in a list.

    Phase 2 iterates over the list, checking whether a link goes to youtube (parse_url is tremendously useful for this), and putting a suitable replacement into a second list.

    Phase 3: you now have two lists, one containing the original matches, one containing the desired replacements. Run str_replace over your original text, providing the match list for the search parameter and the replacement list for the replacements.

    There are several advantages to this approach:

    1. The regular expression for extracting links can be kept relatively simple, since it doesn’t have to take special hostnames into account
    2. It is easier to debug; you can dump the search and replace arrays prior to phase 3, and see if they contain what you expect
    3. Because you perform all replacements in one go, you avoid problems with overlapping matches or replacing a piece of already-replaced text (after all, the replaced text still contains a URL, and you don’t want to replace that again)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data from a table in a database (string) that contain text and
If you have binary strings (literally String objects that contain only 1's and 0's),
I have trouble using Perl grep() with a string that may contain chars that
I have a Dictionary<string,int> that has the potential to contain upwards of 10+ million
I have a string that contains both double-quotes and backslashes that I want to
If I have a string that contains a url (for examples sake, we'll call
I have a string array, and it may contain an element with the text
I have a string that can be very long and contain various lines and
I have a string that should contain a list of items in the form
hi all i have a string text that may contains urls (this text is

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.