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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:44:48+00:00 2026-05-27T09:44:48+00:00

For normal link $content = preg_replace(‘@(https?://([-\w\.]+)+(:\d+)?((/[\w/_\.%\-+~]*)?(\?\S+)?)?)@’, ‘<a href=$1 target=_blank>$1</a>’, $content); For Download link $content

  • 0

For normal link

$content = preg_replace('@(https?://([-\w\.]+)+(:\d+)?((/[\w/_\.%\-+~]*)?(\?\S+)?)?)@', '<a href="$1" target="_blank">$1</a>', $content);

For Download link

$content = preg_replace('@(https?://([-\w\.]+)+(:\d+)?/[\w/_\.%\-+~]+(\.exe|\.mp3|\.pdf|\.rar|\.doc|\.jar|\.ppt|\.xls|\.png|\.jpg|\.jpeg|\.gif|\.mp4|\.avi|\.mkv|\.flv|\.3gp|\.mms|\.sis|\.sisx|\.jad|\.mdi|\.qcp)(\?\S+)?)@', '<a href="$1" target="_blank">Download</a>', $content);

For Youtube Link

// Replace Youtube URLs with embed code
function embedYoutube($text)
{

$search = '%          # Match any youtube URL in the wild.
    (?:https?://)?    # Optional scheme. Either http or https
    (?:www\.)?        # Optional www subdomain
    (?:               # Group host alternatives
      youtu\.be/      # Either youtu.be,
    | youtube\.com    # or youtube.com
      (?:             # Group path alternatives
        /embed/       # Either /embed/
      | /v/           # or /v/
      | /watch\?v=    # or /watch\?v=
      )               # End path alternatives.
    )                 # End host alternatives.
    ([\w\-]{10,12})   # Allow 10-12 for 11 char youtube id.
    \b                # Anchor end to word boundary.
    %x';
    $replace = '<object width="425" height="344">
    <param name="movie" value="http://www.youtube.com/v/$1?fs=1"</param>
    <param name="allowFullScreen" value="true"></param>
    <param name="allowScriptAccess" value="always"></param>
    <embed src="http://www.youtube.com/v/$1?fs=1"
        type="application/x-shockwave-flash" allowscriptaccess="always" allowFullScreen="true" width="425" height="344">
    </embed>
    </object>';

  return preg_replace($search, $replace, $text);
 }

I’m using this but I want to apply all of these on same text, while not affecting output of each other.

e.g.

$content = "Download this song http://songs.com/song1.mp3  
Watch it Video http://www.youtube.com/watch?v=YR12Z8f1Dh8  
and click here for more details http://en.wikipedia.org/wiki/Song"

will produce this

Download this song <br />
<a href="http://songs.com/song1.mp3" target="_blank">Download</a>
Watch it Video <br />
<object width="425" height="344">
    <param name="movie" value="http://www.youtube.com/v/YR12Z8f1Dh8?fs=1"</param>
    <param name="allowFullScreen" value="true"></param>
    <param name="allowScriptAccess" value="always"></param>
    <embed src="http://www.youtube.com/v/YR12Z8f1Dh8?fs=1"
        type="application/x-shockwave-flash" allowscriptaccess="always" allowFullScreen="true" width="425" height="344">
    </embed>        </object>
and click here for more details <br />
<a href="http://en.wikipedia.org/wiki/Song" target="_blank">http://en.wikipedia.org/wiki/Song</a> 

Can I solve this with a common regular expression or by any PHP methodology?

  • 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-27T09:44:49+00:00Added an answer on May 27, 2026 at 9:44 am

    I would use preg_match_all with the PREG_OFFSET_CAPTURE option to find all of the URLs with their offsets, rewrite each URL, use the offsets to extract the interstitial text, and put everything back together.

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

Sidebar

Related Questions

I'd like to convert a normal link tag into an image tag, How would
I want to have a delete user link in a normal Activerecord table, but
Edit: Here is the new code: $(#normalcontent).hide(fast).load($this.attr(href) + #normalcontent,,function(){ $(this).slideDown(normal); $(this).find(img).each(function(){ if($(this).hasClass(large)) { var
Given a normal nhibernate config file: <?xml version=1.0?> <?xml-stylesheet type=text/xsl href=http://localhost/xmlStylesheets/nhibernate.xsl?> <hibernate-configuration xmlns=urn:nhibernate-configuration-2.2> <session-factory>
How do i grab the link href (under the published node) from this xml:
Normal page looks like this: http://hell.orts.ru:8004/snapboard/threads/id/3/ However, if …#fragment_id is used in the link,
My normal work flow to create a new repository with subversion is to create
Sometimes normal FTP doesn't quite cut it... When you need to do secure FTP
In normal WebForms scenario, any root-relative URLs (e.g. ~/folder/file.txt) inside CSS files such as:
Under normal circumstances, a VB.NET application of mine can check the ClientName environmental variable

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.