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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:16:50+00:00 2026-06-17T18:16:50+00:00

I am having troubles spliting a string as followed : what is text to

  • 0

I am having troubles spliting a string as followed :
what is text to remain a text what is link to be transformed in anchor and what is link from youtube to convert it to iframe tag.

My script looks like this :

$string='This is a link www.dinamomania.net this is a http://www.youtube.com/watch?v=U9LB6qGvdpQ';
echo makelink($string);

 function makeLink($string){

 /*** make sure there is an http:// on all URLs ***/
 $string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string);
 /*** make all URLs links ***/

$string = preg_replace('/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i','<a target="_blank" href="$1">$1</A>',$string);

$string = preg_replace('/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/', '<iframe style= "margin:15px 0 15px 0;display:block;" width="500" height="300" src="http://www.youtube.com/embed/$7" frameborder="0" allowfullscreen></iframe>',$string);

 /*** make all emails hot links ***/

 $string = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}[0-9]{1,3})(\]?))/i","<A HREF=\"mailto:$1\">$1</A>",$string);

 return $string;

}

}

Everything it’s working how it’s supposed to just that, i get an anchor like this “> before the iframe, thats because first it’s doing the link into anchors and then the iframe for youtube format. So it’s an inframe into an anchor.

I was looking for an if statement to do something like this :

if( is link from youtube ) {
    // do the iframe part 
} else {
    // do the anchor part
}

Any help/advice will be highly appreciated. Thanks !

I have came with something like this :

    if (preg_match("/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/", $string))
    {
$string = preg_replace('/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/', '<iframe style= "margin:15px 0 15px 0;display:block;" width="500" height="300" src="http://www.youtube.com/embed/$7" frameborder="0" allowfullscreen></iframe>',$string);
    } else {
$string = preg_replace('/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i','<a target="_blank" href="$1">$1</A>',$string);
    }

But yet again it’s doing my iframe part and nothing happends with anchors.

  • 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-17T18:16:52+00:00Added an answer on June 17, 2026 at 6:16 pm

    Use this pattern for URL

    preg_replace('/([^\'"])((ht|f)tps?:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i',
        '$1<A target="_blank" href="$2">$2</A>',$string);
    

    And put the IFRAME placing statement before A tag placing statement.

        /*** make all the IFrame links ***/
    $string = preg_replace(
    '/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/', 
    '<iframe style= "margin:15px 0 15px 0;display:block;" width="500" height="300" src="http://www.youtube.com/embed/$7" frameborder="0" allowfullscreen></iframe>',
    $string);
    
    /*** make all URLs links ***/
    $string = preg_replace('/([^\'"])((ht|f)tps?:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i',
            '$1<A target="_blank" href="$2">$2</A>',$string);
    

    See how it works.

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

Sidebar

Related Questions

I'm having troubles to get minute from a string with MINUTE() function, it always
im having troubles with adding a class to the last option from foreach, its
I am having troubles trying to load the data from all the rows of
Im having troubles with displaying one Event information from my database. After clicking on
Currently having troubles getting results from a simple aggregate function with HQL. Here is
I am having troubles receiving the string I am sending with a UDP packet
I'm having troubles finding a string into a listbox, my string NombreCompleto is made
Im having troubles sending a variable from php to javascript. I pull javascript (Ajax)
i am having trouble splitting a string in c# have a string (text in
i'm having troubles splitting a .txt file that i exported from a db. The

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.