PHP automatically adds http: // header conversion URL as link _ PHP Tutorial

Source: Internet
Author: User
PHP automatically adds the header conversion URL as a link. Sometimes, when a user needs to enter a URL, we usually let the user omit it. after the submission is complete, the code is automatically added. if necessary, we can also, when you need to enter the URL, we usually let the user omit "http: //". after the submission is complete, the code will automatically add http ://, if necessary, we can also convert the URL into a link, similar to the functions in many web editor, the following code will implement this type of function. Let's take a look at the code that automatically adds the "http: //" header:

PHP function code that automatically adds the "http: //" header:

1

2if (! Preg_match ("/^ (http | ftp):/", $ _ POST ['URL']) {

3 $ _ POST ['URL'] = 'http: // '. $ _ POST ['URL'];

4}

5?>

PHP converts the URL string to a hyperlink. you can convert the URL string and the e-mail address string to a clickable hyperlink:

01

02 function makeClickableLinks ($ text ){

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

04 '\ 1', $ text );

05 $ text = eregi_replace ('([[: space:] () [{}]) (www. [-a-zA-Z0-9 @: % _ + .~ #? & // =] + )',

06 '\ 1 \ 2', $ text );

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

08 '\ 1', $ text );

09 return $ text;

10}

11?>

By combining the two codes, you can use the following code:

View sourceprint? 1

2 $ _ POST ['URL'] = "www.codefans.net ";

3if (! Preg_match ("/^ (http | ftp):/", $ _ POST ['URL']) {

4 $ url = 'http: // '. $ _ POST ['URL'];

5}

6 echo makeClickableLinks ($ url );

7?>

The final effect is to add http: // to www.codefans.net 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.