This article mainly introduces nine practical PHP code snippets, this article explains how to check whether emails have been read, extract keywords from webpages, search for all links on the page, and automatically convert URLs into clickable hyperlinks,
This article mainly introduces nine practical PHP code snippets, this article explains how to check whether emails have been read, extract keywords from webpages, search for all links on the page, and automatically convert URLs into clickable hyperlinks,
1. Check whether the email has been read
When you send an email, you are sure to know whether your email has been viewed by the recipient. The following code records the IP address of your email, as well as the actual reading date and time.
The Code is as follows:
Error_reporting (0 );
Header ("Content-Type: image/jpeg ");
// Get IP
If (! Empty ($ _ SERVER ['HTTP _ CLIENT_IP '])
{
$ Ip = $ _ SERVER ['HTTP _ CLIENT_IP '];
}
Elseif (! Empty ($ _ SERVER ['HTTP _ X_FORWARDED_FOR '])
{
$ Ip = $ _ SERVER ['HTTP _ X_FORWARDED_FOR '];
}
Else
{
$ Ip = $ _ SERVER ['remote _ ADDR '];
}
// Time
$ Actual_time = time ();
$ Actual_day = date ('Y. m. D', $ actual_time );
$ Actual_day_chart = date ('d/m/Y', $ actual_time );
$ Actual_hour = date ('H: I: s', $ actual_time );
// GET Browser
$ Browser = $ _ SERVER ['HTTP _ USER_AGENT '];
// LOG
$ MyFile = "log.txt ";
$ Fh = fopen ($ myFile, 'a + ');
$ StringData = $ actual_day. ''. $ actual_hour.''. $ ip. ''. $ browser.''. "\ r \ n ";
Fwrite ($ fh, $ stringData );
Fclose ($ fh );
// Generate Image (Es. dimesion is 1x1)
$ Newimage = ImageCreate (1, 1 );
$ Grigio = ImageColorAllocate ($ newimage, 255,255,255 );
ImageJPEG ($ newimage );
ImageDestroy ($ newimage );
?>
Source code:
2. Extract keywords from webpages
This excellent code can be used to extract keywords from webpages.
The Code is as follows:
$ Meta = get_meta_tags ('HTTP: // www.emoticode.net /');
$ Keywords = $ meta ['keyword'];
// Split keywords
$ Keywords = explode (',', $ keywords );
// Trim them
$ Keywords = array_map ('trim', $ keywords );
// Remove empty values
$ Keywords = array_filter ($ keywords );
Print_r ($ keywords );
Source code:
3. Search for all links on the page
With DOM, you can capture links on any page, as shown in the following example.
The Code is as follows:
$ Html = file_get_contents ('HTTP: // www.php100.com ');
$ Dom = new DOMDocument ();
@ $ Dom-> loadHTML ($ html );
// Grab all the on the page
$ Xpath = new DOMXPath ($ dom );
$ Hrefs = $ xpath-> evaluate ("/html/body // ");
For ($ I = 0; $ I <$ hrefs-> length; $ I ++ ){
$ Href = $ hrefs-> item ($ I );
$ Url = $ href-> getAttribute ('href ');
Echo $ url .'';
}
Source code:
4. automatically convert URLs to clickable hyperlinks
In Wordpress, If You Want To automatically convert all URLs to clickable hyperlinks, you can use the built-in function make_clickable. When you operate outside WordPress, you can refer to the source code in wp-uplodes/formatting. php.
The Code is as follows:
Function _ make_url_clickable_cb ($ matches ){
$ Ret = '';
$ Url = $ matches [2];
If (empty ($ url ))
Return $ matches [0];
// Removed trailing [.,;:] from URL
If (in_array (substr ($ url,-1), array ('.', ';', ':') = true ){
$ Ret = substr ($ url,-1 );
$ Url = substr ($ url, 0, strlen ($ url)-1 );
}
Return $ matches [1]. "$ url". $ ret;
}
Function _ make_web_ftp_clickable_cb ($ matches ){
$ Ret = '';
$ Dest = $ matches [2];
$ Dest = 'HTTP: // '. $ dest;
If (empty ($ dest ))
Return $ matches [0];
// Removed trailing [,;:] from URL
If (in_array (substr ($ dest,-1), array ('.', ';', ':') = true ){
$ Ret = substr ($ dest,-1 );
$ Dest = substr ($ dest, 0, strlen ($ dest)-1 );
}
Return $ matches [1]. "$ dest". $ ret;
}
Function _ make_email_clickable_cb ($ matches ){
$ Email = $ matches [2]. '@'. $ matches [3];
Return $ matches [1]. "$ email ";
}
Function make_clickable ($ ret ){
$ Ret = ''. $ ret;
// In testing, using arrays here was found to be faster
$ Ret = preg_replace_callback ('# ([\ s>]) ([\ w] +? : // [\ W \ x80-\ xff \ # $ % &~ /. \-;: = ,? @ \ [\] +] *) # Is ',' _ make_url_clickable_cb ', $ ret );
$ Ret = preg_replace_callback ('# ([\ s>]) (www | ftp) \. [\ w \ x80-\ xff \ # $ % &~ /. \-;: = ,? @ \ [\] +] *) # Is ',' _ make_web_ftp_clickable_cb ', $ ret );
$ Ret = preg_replace_callback ('# ([\ s>]) ([. 0-9a-z _ +-] +) @ ([0-9a-z-] + \.) + [0-9a-z] {2,}) # I ',' _ make_email_clickable_cb ', $ ret );
// This one is not in an array because we need it to run last, for cleanup of accidental links within links
$ Ret = preg_replace ("# (] +?> |>)] +?> ([^>] + ?) # I "," $1 $3 ", $ ret );
$ Ret = trim ($ ret );
Return $ ret;
}
Source code:
5. Create a data URI
Data URI can help embed images into HTML, CSS, and JS to save HTTP requests. The following functions can use $ file to create a data URI.
The Code is as follows: