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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:08:21+00:00 2026-06-01T22:08:21+00:00

I need a form to autolink links that users input in text fields. I

  • 0

I need a form to autolink links that users input in text fields. I found an example on stack which works perfectly, except for one thing. if the user inputs a link without including http:// or https:// and instead starts the link only using www. the link does not work properly.

ie a user input would be

check out our twitter!
www.twitter.com/#!/profile

and our facebook!
https://www.facebook.com/profile

the output would be

check out our twitter!
<a href="www.twitter.com/#!/profile">www.twitter.com/#!/profile</a>

and our facebook!
<a href="https://www.facebook.com/profile">http://www.facebook.com/profile</a>

so the facebook link works perfectly, but the twitter one would not, as its being linked to the current location the user is on plus the new link, ie if they are currently on www.example.com the link would become www.example.com/www.twitter.com/#!/profile

for the life of me, i cant figure out how to fix this by simply adding http:// to the beginning of the link, this is the function:

function auto_link_text($text) {
    $pattern  = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
    return preg_replace_callback($pattern, 'auto_link_text_callback', $text);
}

function auto_link_text_callback($matches) {
     $max_url_length = 50;
    $max_depth_if_over_length = 2;
    $ellipsis = '&hellip;';

    $url_full = $matches[0];
    $url_short = '';

    if (strlen($url_full) > $max_url_length) {
        $parts = parse_url($url_full);
        $url_short = $parts['scheme'] . '://' . preg_replace('/^www\./', '',  $parts['host']) . '/';

    $path_components = explode('/', trim($parts['path'], '/'));
    foreach ($path_components as $dir) {
        $url_string_components[] = $dir . '/';
    }

    if (!empty($parts['query'])) {
        $url_string_components[] = '?' . $parts['query'];
    }

    if (!empty($parts['fragment'])) {
        $url_string_components[] = '#' . $parts['fragment'];
    }

    for ($k = 0; $k < count($url_string_components); $k++) {
        $curr_component = $url_string_components[$k];
        if ($k >= $max_depth_if_over_length || strlen($url_short) + strlen($curr_component) > $max_url_length) {
            if ($k == 0 && strlen($url_short) < $max_url_length) {
                // Always show a portion of first directory
                $url_short .= substr($curr_component, 0, $max_url_length - strlen($url_short));
            }
            $url_short .= $ellipsis;
            break;
        }
        $url_short .= $curr_component;
    }

} else {
    $url_short = $url_full;
}

    return "<a rel=\"nofollow\" href=\"$url_full\">$url_short</a>";
}
  • 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-01T22:08:24+00:00Added an answer on June 1, 2026 at 10:08 pm

    Use strpos function.
    if the input contains “http://” forward directly. Otherwise add it direct it.

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

Sidebar

Related Questions

I have input from Form Name,Branch (text) x,y and Total are (number) fields Record
I have an entity that has three fields that need to form a unique
I have multiple MVC 3 forms that need form validation on certain fields (mostly
In my web-application i have a form which contains about 25 fields. Users will
I need the form that will edit the array of entities and their related
I need a form to enable user to enter event info in three fields
In my form i need to select an item (which is a div with
I need to post a form on a new website which is UTF-8 encoded.
I'm using sfDoctrineAllowPlugin. I need form which has Twitter's Bootstrap semantics. I looked into
Preface: I'm not sure what resources are. I need this form (which is working

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.