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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:49:22+00:00 2026-06-06T07:49:22+00:00

I created a PHP function that will automatically turn URLs into links, link Twitter

  • 0

I created a PHP function that will automatically turn URLs into links, link Twitter @reply usernames to its Twitter profile, and turn email addresses into links with the mailto protocol.

Here is the script:

function autolink($a, $b){
        $e = "";
        $f = array(
            'link' => '~(http(s|)\:\/\/(www\.|)((\w+)\.(\w+)(\:[0-9]{2,5}|)\.[a-z]{2,5}|(\w+)(\:[0-9]{2,5}|)\.[a-z]{2,5}))((((\/|)\w+)(\.[a-z]{2,5}|))+)(\?(\w+\=\w+(\&|))+|)~',
            'email' => '~\w+\@\w+(\:[0-9]{2,5}|)\.[a-z]{2,5}~',
            'twitter' => '~\@([a-zA-Z_0-9]){1,15}~'
        );
    switch ($b) {
    case "link":$e = preg_replace_callback($f['link'], function ($a) {return '<a href="'.$a.'" rel="nofollow" target="_blank">'.$a.'</a>';},$a);break;
    case "email":$e = preg_replace_callback($f['email'], function ($a) {return '<a href="mailto:'.$a.'" rel="nofollow" target="_blank">'.$a.'</a>';},$a);break;
    case "twitter":$e = preg_replace_callback($f['twitter'], function ($a) {return '<a href="https://twitter.com/#!/'.str_replace('@','',$a).'" rel="nofollow" target="_blank">'.$a.'</a>';},$a);break;
    }
    return $e;
}

The only issue I’m having is that instead of returning the link it returns the word: “Array”.

For example, this:

autolink("This is my site http://weebuild.biz", "link");

Is returning this:

This is my site Array

When it should be returning this:

This is my site <a href="http://weebuild.biz" rel="nofollow"
target="_blank">http://weebuild.biz</a>

The original script was in JavaSript which I wrote as well: http://jsfiddle.net/shawn31313/umgqR/2/

Since I’m not a PHP developer the JavaScript version has some more features.

Maybe the issue is that I took the regular expression from JavaScript and just put it in the PHP. I’m not sure if regular expressions in PHP are different from JavaScript.

Thanks in advance.

  • 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-06T07:49:24+00:00Added an answer on June 6, 2026 at 7:49 am

    Try the following change:

    case "link":$e = preg_replace_callback(
            $f['link'],
            function ($a) {
                return '<a href="'.$a[0].'" rel="nofollow" target="_blank">'.$a[0].'</a>';
            },
            $a
         );
         break;
    

    The issue is that the argument to the callback will be an array, not a string.

    From the docs:

    A callback that will be called and passed an array of matched elements
    in the subject string. The callback should return the replacement
    string.

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

Sidebar

Related Questions

I created a jQuery function that calls on a PHP server-side file that pulls
I have created a function in PHP that calls a webservice and parses through
Is there a PHP function that will do the following: Move a specified array
Hello I have a jQuery function that will send to a php file some
I am creating a PHP function that will take some values, one of which
I am intending to create a small api, that will do some php functions,
I'm looking to create a PHP function that can trim each line in a
I would like to know how to create a php function that can be
i'm referring this address for function olLiTree PHP function that creates a nested ul
I'm working in PHP and I want to create a function that, given a

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.