For use directly, super practical PHP code snippet (2) _ PHP Tutorial

Source: Internet
Author: User
Use it directly. a Practical PHP code snippet (2 ). Every Programmer and developer likes to discuss their favorite code snippets, especially when PHP developers spend several hours coding webpages or creating applications, they know more about the code. every programmer and developer prefers to discuss their favorite code snippets, especially when PHP developers spend several hours coding webpages or creating applications, they are more aware of the importance of the code. To save coding time, I collected some useful code snippets to help developers improve their work efficiency. >>> Click to view PHP code snippets (1)

1) Whois query using PHP -- use PHP to retrieve Whois requests

Using this code, you can obtain whois information in a specific domain name. Take the domain name as a parameter and display the relevant information of all domain names.


  1. Function whois_query ($ domain ){
  2. // Fix the domain name:
  3. $ Domain = strtolower (trim ($ domain ));
  4. $ Domain = preg_replace ('/^ http: \\// I', '', $ domain );
  5. $ Domain = preg_replace ('/^ www \./I', '', $ domain );
  6. $ Domain = explode ('/', $ domain );
  7. $ Domain = trim ($ domain [0]);
  8. // Split the TLD from domain name
  9. $ _ Domain = explode ('.', $ domain );
  10. $ Lst = count ($ _ domain)-1;
  11. $ Ext = $ _ domain [$ lst];
  12. // You find resources and lists
  13. // Like these on wikipedia:
  14. //
  15. // Http://de.wikipedia.org/wiki/Whois </a>
  16. //
  17. $ Servers = array (
  18. "Biz" => "whois. neulevel. biz ",
  19. "Com" => "whois.internic.net ",
  20. "Us" => "whois. nic. us ",
  21. "Coop" => "whois. nic. coop ",
  22. "Info" => "whois.nic.info ",
  23. "Name" => "whois. nic. name ",
  24. "Net" => "whois.internic.net ",
  25. "Gov" => "whois.nic.gov ",
  26. "Edu" => "whois.internic.net ",
  27. "Mil" => "rs.internic.net ",
  28. "Int" => "whois.iana.org ",
  29. "Ac" => "whois. nic. ac ",
  30. "AE" => "whois. uaenic. AE ",
  31. "At" => "whois.ripe.net ",
  32. "Au" => "whois.aunic.net ",
  33. "Be" => "whois. dns. be ",
  34. "Bg" => "whois.ripe.net ",
  35. "Br" => "whois.registro.br ",
  36. "Bz" => "whois. belizenic. bz ",
  37. "Ca" => "whois. cira. ca ",
  38. "Cc" => "whois. nic. cc ",
  39. "Ch" => "whois. nic. ch ",
  40. "Cl" => "whois. nic. cl ",
  41. "Cn" => "whois.cnnic.net.cn ",
  42. "Cz" => "whois. nic. cz ",
  43. "De" => "whois.nic.de ",
  44. "Fr" => "whois. nic. fr ",
  45. "Hu" => "whois. nic. hu ",
  46. "Ie" => "whois. domainregistry. ie ",
  47. "Il" => "whois.isoc.org. il ",
  48. "In" => "whois. ncst. ernet. in ",
  49. "Ir" => "whois. nic. ir ",
  50. "Mc" => "whois.ripe.net ",
  51. "To" => "whois. tonic. ",
  52. "TV" => "whois. TV ",
  53. "Ru" => "whois.ripn.net ",
  54. "Org" => "whois.pir.org ",
  55. "Aero" => "whois. information. aero ",
  56. "Nl" => "whais. domain-registry.nl"
  57. );
  58. If (! Isset ($ servers [$ ext]) {
  59. Die ('Error: No matching nic server found! ');
  60. }
  61. $ Nic_server = $ servers [$ ext];
  62. $ Output = '';
  63. // Connect to whois server:
  64. If ($ conn = fsockopen ($ nic_server, 43 )){
  65. Fputs ($ conn, $ domain. "\ r \ n ");
  66. While (! Feof ($ conn )){
  67. $ Output. = fgets ($ conn, 128 );
  68. }
  69. Fclose ($ conn );
  70. }
  71. Else {die ('Error: cocould not connect to '. $ nic_server .'! ');}
  72. Return $ output;
  73. }
2) Text messaging with PHP using the TextMagic API -- use TextMagic API to get PHP Test information

TextMagic introduces powerful core APIs to easily send SMS messages to mobile phones. This API is billed.


  1. The TextMagic PHP lib
  2. Require ('textmagic-sms-api-php/TextMagicAPI. php ');
  3. // Set the username and password information
  4. $ Username = 'myusername ';
  5. $ Password = 'mypassword ';
  6. // Create a new instance of TM
  7. $ Router = new TextMagicAPI (array (
  8. 'Username' => $ username,
  9. 'Password' => $ password
  10. ));
  11. // Send a text message to '2017-123-4567'
  12. $ Result = $ router-> send ('Wake up! ', Array (9991234567), true );
  13. // Result: Result is: Array ([messages] => Array ([19896128] => 9991234567) [sent_text] => Wake up! [Parts_count] => 1)
3) Get info about your memory usage -- Get the memory usage code to help you Get the memory usage.


  1. Echo "Initial:". memory_get_usage (). "bytes \ n ";
  2. /* Prints
  3. Initialize: 361400 bytes
  4. */
  5. // Let's use up some memory
  6. For ($ I = 0; I I <100000; $ I ++ ){
  7. $ Array [] = md5 ($ I );
  8. }
  9. // Let's remove half of the array
  10. For ($ I = 0; I I <100000; $ I ++ ){
  11. Unset ($ array [$ I]);
  12. }
  13. Echo "Final:". memory_get_usage (). "bytes \ n ";
  14. /* Prints
  15. Final: 885912 bytes
  16. */
  17. Echo "Peak:". memory_get_peak_usage (). "bytes \ n ";
  18. /* Prints
  19. Peak: 13687072 bytes
  20. */
4) Display source code of any webpage -- view the source code of any web page

If you want to view the source code of the webpage, you only need to change the URL of the second line and the source code will be displayed on the webpage.


  1. $ Line ){
  2. // Loop thru each line and prepend line numbers
  3. Echo "Line # {$ line_num}:". htmlspecialchars ($ line )."
  4. \ N ";
  5. }

5) Create data uri's -- Create a data uri by using this code, you can Create a data Uri, which is very useful for embedding images in HTML/CSS and can help save HTTP requests.


  1. Function data_uri ($ file, $ mime ){
  2. $ Contents = file_get_contents ($ file );
  3. $ Base64 = base64_encode ($ contents );
  4. Echo "data: $ mime; base64, $ base64 ";
  5. }
6) Detect location by IP -- retrieve the geographical location through IP

This code helps you find a specific IP address. you only need to enter an IP address in the function parameter to check the location.


  1. Function detect_city ($ ip ){
  2. $ Default = 'unknown ';
  3. If (! Is_string ($ ip) | strlen ($ ip) <1 | $ ip = '2017. 0.0.1 '| $ ip = 'localhost') $ ip = '8. 8.8.8 '; $ curlopt_useragent = 'mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: 1.9.2) Gecko/20100115 Firefox/3.6 (. net clr 3.5.30729) '; $ url = 'http: // ipinfodb.com/ip_locator.php? Ip = '. urlencode ($ ip); $ ch = curl_init (); $ curl_opt = array (CURLOPT_FOLLOWLOCATION => 1,
  4. CURLOPT_HEADER => 0,
  5. CURLOPT_RETURNTRANSFER => 1,
  6. CURLOPT_USERAGENT => $ curlopt_useragent,
  7. CURLOPT_URL => $ url,
  8. CURLOPT_TIMEOUT => 1,
  9. CURLOPT_REFERER => 'http: // '. $ _ SERVER ['http _ host'],
  10. );
  11. Curl_setopt_array ($ ch, $ curl_opt );
  12. $ Content = curl_exec ($ ch );
  13. If (! Is_null ($ curl_info )){
  14. $ Curl_info = curl_getinfo ($ ch );
  15. }
  16. Curl_close ($ ch );
  17. If (preg_match ('{
  18. City: ([^ <] *)
  19. } I ', $ content, $ regs) {$ city = $ regs [1];} if (preg_match ('{
  20. State/Province: ([^ <] *)
  21. } I ', $ content, $ regs) {$ state = $ regs [1];} if ($ city! = "& $ State! = ") {$ Location = $ city. ','. $ state; return $ location;} else {return $ default ;}}
7) Detect browser language -- view the code script language used by the browser to Detect the browser.


  1. Function get_client_language ($ availableLanguages, $ default = 'en '){
  2. If (isset ($ _ SERVER ['http _ ACCEPT_LANGUAGE ']) {
  3. $ Langs = explode (',', $ _ SERVER ['http _ ACCEPT_LANGUAGE ']);
  4. Foreach ($ langs as $ value ){
  5. $ Choice = substr ($ value, 0, 2 );
  6. If (in_array ($ choice, $ availableLanguages )){
  7. Return $ choice;
  8. }
  9. }
  10. }
  11. Return $ default;
  12. }
8) Check if server is HTTPS -- Check whether the server is HTTPS
  1. If ($ _ SERVER ['https']! = "On "){
  2. Echo "This is not HTTPS ";
  3. } Else {
  4. Echo "This is HTTPS ";
  5. }
9) Generate CSV file from a PHP array--generate a. CSV file in the PHP Group
  1. Function generateCsv ($ data, $ delimiter = ',', $ enclosure = '"'){
  2. $ Handle = fopen ('php: // temp ', 'R + ');
  3. Foreach ($ data as $ line ){
  4. Fputcsv ($ handle, $ line, $ delimiter, $ enclosure );
  5. }
  6. Rewind ($ handle );
  7. While (! Feof ($ handle )){
  8. $ Contents. = fread ($ handle, 8192 );
  9. }
  10. Fclose ($ handle );
  11. Return $ contents;
  12. }
From: Designzum

...

Related Article

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.