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.