Convert the URL address in the text into JavaScript, PHP custom functions, and urljavascript
When writing a small program these days, you need to use a regular expression to match the URL address in the user input text, and then replace the URL address with a clickable link. URL address matching. I think this is often used in verification. Here we will give a complete expression that I have integrated:
Copy codeThe Code is as follows:
Var URL =/(https? : \/| Ftps? :\/\/)? (\ D {1, 3} \. \ d {1, 3} \. \ d {1, 3}) (: [0-9] + )? | (Localhost) (: [0-9] + )? | ([\ W] + \.) (\ S +) (\ w {2, 4}) (: [0-9] + )?) (\/? ([\ W #! :.? + = & % @! \-\/] + ))? /Ig;
This expression can match the URL addresses of http, https, ftp, ftps, and IP addresses. It is still well-regarded for URL address matching. Using this expression, I wrote two small functions to replace the URL address of the user's message with the clickable link. Nothing is too difficult, that is, using the replace () of JavaScript () function to replace the URL with link:
JavaScript version:
Copy codeThe Code is as follows:
/**
* JavaScrit version
* Convert the URL address to the complete A tag link code
*/
Var replaceURLToLink = function (text ){
Text = text. replace (URL, function (url ){
Var urlText = url;
If (! Url. match ('^ https? :\/\/')){
Url = 'HTTP: // '+ url;
}
Return ''+ urlText + '';
});
Return text;
};
PHP version:
Copy codeThe Code is as follows:
/**
* The PHP version is modified based on the Silva code
* Convert the URL address to the complete A tag link code
*/
/** =================================================== ==========
NAME: replace_URLtolink ()
VERSION: 1.0
AUTHOR: J de Silva
DESCRIPTION: returns VOID; handles converting
URLs into clickable links off a string.
TYPE: functions
========================================================== ===== */
Function replace_URLtolink ($ text ){
// Grab anything that looks like a URL...
$ Urls = array ();
// Build the patterns
$ Scheme = '(https? \: \/| Ftps? \:\/\/)? ';
$ Www = '([\ w] + \.)';
$ Local = 'localhost ';
$ Ip = '(\ d {1, 3} \. \ d {1, 3} \. \ d {1, 3} \. \ d {1, 3 })';
$ Name = '([\ w0-9] + )';
$ Tld = '(\ w {2, 4 })';
$ Port = '(: [0-9] + )? ';
$ The_rest = '(\/? ([\ W #! :.? + = & % @! \-\/] + ))? ';
$ Pattern = $ scheme. '('. $ ip. $ port. '| '. $ www. $ name. $ tld. $ port. '| '. $ local. $ port. ')'. $ the_rest;
$ Pattern = '/'. $ pattern. '/is ';
// Get the URLs
$ C = preg_match_all ($ pattern, $ text, $ m );
If ($ c ){
$ Urls = $ m [0];
}
// Replace all the URLs
If (! Empty ($ urls )){
Foreach ($ urls as $ url ){
$ Pos = strpos ('HTTP \: \/', $ url );
If ($ pos & $ pos! = 0) |! $ Pos ){
$ Fullurl = 'HTTP: // '. $ url;
} Else {
$ Fullurl = $ url;
}
$ Link = ''. $ url .'';
$ Text = str_replace ($ url, $ link, $ text );
}
}
Return $ text;
}
In JavaScript, how does one click a hyperlink to execute a function and pass the hyperlink text as a function parameter?
<A href = "javascript: void (0)" onclick = "chaXunCheCi (this. innerHTML)"> path 538 </a>
<Script>
Function chaXunCheCi (checi ){
Alert (checi) // checi is the parameter you want to pass in
// Omitted...
}
</Script>
Is that what it means?
Our predecessors converted the following JavaScript udfs into PHP udfs,
1: use this javascript code to determine whether the link is available. Some invalid URLs will be corrected and transferred to other pages. How can this problem be solved?
It is too difficult to judge the validity of a link. I have found a lot of information and have not found any useful information. One saying is to judge the returned header information, but I don't know how to implement it technically, another argument is to use time to determine the response. If a page does not exist, the response takes a long time (the page exists, and the response is usually 1-2 s, if it is not large, it is 7-10 s, or more), but this is not reliable.
If you can determine, you can directly change the href attribute of <a> to another page when an invalid URL is encountered!
2: How do I call javascript Functions in php functions?
I don't think it can be accessed. Although php is a server language, it has been converted into a client language during client access. Naturally, it can be accessed!
It is best to use event trigger when calling (not called when not needed), as described in the first floor!