PHP automatically add http://header convert URL to link

Source: Internet
Author: User
Tags ftp

Sometimes, when we need the user to enter the URL, generally we will let users omit the "http://", when the submission is completed with the code automatically plus http://, if necessary, we can also convert the URL into the form of links, similar to the many web page editor functions, the following code will implement this type of functionality. First look at the code that automatically adds the "http://" header:

To automatically add the PHP function code for the "http://" header:

1<?php

2if (!preg_match ("/^ (HTTP|FTP):/", $_post[' url ')) {

3 $_post[' url '] = ' http://'. $_post[' url '];

4}

5?>

PHP converts URL strings into hyperlinks to convert URLs and e-mail address strings to clickable hyperlinks:

<?php

02function Makeclickablelinks ($text) {

$text = Eregi_replace ((f|ht) {1}tp://) [-a-za-z0-9@:%_+.~#?&//=]+) ',

' 1 ', $text);

$text = Eregi_replace ([[: Space:] () [{}]) (www.[ -a-za-z0-9@:%_+.~#?&//=]+) ',

' A ', $text);

$text = Eregi_replace (' (_.0-9a-z-]+@) ([0-9a-z][0-9a-z-]+.) +[a-z]{2,3}) ',

1 ', $text);

09return $text;

10}

11?>

Combining these two pieces of code can form the following usage:

View Sourceprint?1

2$_post[' url ']= ' www.codefans.net ';

3if (!preg_match ("/^ (HTTP|FTP):/", $_post[' url ')) {

4 $url = ' http://'. $_post[' url '];

5}

6echo makeclickablelinks ($url);

7?>

The final effect is to add the www.codefans.net to the http://and implement the link form.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.