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.
- Function whois_query ($ domain ){
-
- // Fix the domain name:
- $ Domain = strtolower (trim ($ domain ));
- $ Domain = preg_replace ('/^ http: \\// I', '', $ domain );
- $ Domain = preg_replace ('/^ www \./I', '', $ domain );
- $ Domain = explode ('/', $ domain );
- $ Domain = trim ($ domain [0]);
-
- // Split the TLD from domain name
- $ _ Domain = explode ('.', $ domain );
- $ Lst = count ($ _ domain)-1;
- $ Ext = $ _ domain [$ lst];
-
- // You find resources and lists
- // Like these on wikipedia:
- //
- // Http://de.wikipedia.org/wiki/Whois </a>
- //
- $ Servers = array (
- "Biz" => "whois. neulevel. biz ",
- "Com" => "whois.internic.net ",
- "Us" => "whois. nic. us ",
- "Coop" => "whois. nic. coop ",
- "Info" => "whois.nic.info ",
- "Name" => "whois. nic. name ",
- "Net" => "whois.internic.net ",
- "Gov" => "whois.nic.gov ",
- "Edu" => "whois.internic.net ",
- "Mil" => "rs.internic.net ",
- "Int" => "whois.iana.org ",
- "Ac" => "whois. nic. ac ",
- "AE" => "whois. uaenic. AE ",
- "At" => "whois.ripe.net ",
- "Au" => "whois.aunic.net ",
- "Be" => "whois. dns. be ",
- "Bg" => "whois.ripe.net ",
- "Br" => "whois.registro.br ",
- "Bz" => "whois. belizenic. bz ",
- "Ca" => "whois. cira. ca ",
- "Cc" => "whois. nic. cc ",
- "Ch" => "whois. nic. ch ",
- "Cl" => "whois. nic. cl ",
- "Cn" => "whois.cnnic.net.cn ",
- "Cz" => "whois. nic. cz ",
- "De" => "whois.nic.de ",
- "Fr" => "whois. nic. fr ",
- "Hu" => "whois. nic. hu ",
- "Ie" => "whois. domainregistry. ie ",
- "Il" => "whois.isoc.org. il ",
- "In" => "whois. ncst. ernet. in ",
- "Ir" => "whois. nic. ir ",
- "Mc" => "whois.ripe.net ",
- "To" => "whois. tonic. ",
- "TV" => "whois. TV ",
- "Ru" => "whois.ripn.net ",
- "Org" => "whois.pir.org ",
- "Aero" => "whois. information. aero ",
- "Nl" => "whais. domain-registry.nl"
- );
-
- If (! Isset ($ servers [$ ext]) {
- Die ('Error: No matching nic server found! ');
- }
-
- $ Nic_server = $ servers [$ ext];
-
- $ Output = '';
-
- // Connect to whois server:
- If ($ conn = fsockopen ($ nic_server, 43 )){
- Fputs ($ conn, $ domain. "\ r \ n ");
- While (! Feof ($ conn )){
- $ Output. = fgets ($ conn, 128 );
- }
- Fclose ($ conn );
- }
- Else {die ('Error: cocould not connect to '. $ nic_server .'! ');}
-
- Return $ output;
- }
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.
- The TextMagic PHP lib
- Require ('textmagic-sms-api-php/TextMagicAPI. php ');
-
- // Set the username and password information
- $ Username = 'myusername ';
- $ Password = 'mypassword ';
-
- // Create a new instance of TM
- $ Router = new TextMagicAPI (array (
- 'Username' => $ username,
- 'Password' => $ password
- ));
-
- // Send a text message to '2017-123-4567'
- $ Result = $ router-> send ('Wake up! ', Array (9991234567), true );
-
- // 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.
- Echo "Initial:". memory_get_usage (). "bytes \ n ";
- /* Prints
- Initialize: 361400 bytes
- */
-
- // Let's use up some memory
- For ($ I = 0; I I <100000; $ I ++ ){
- $ Array [] = md5 ($ I );
- }
-
- // Let's remove half of the array
- For ($ I = 0; I I <100000; $ I ++ ){
- Unset ($ array [$ I]);
- }
-
- Echo "Final:". memory_get_usage (). "bytes \ n ";
- /* Prints
- Final: 885912 bytes
- */
-
- Echo "Peak:". memory_get_peak_usage (). "bytes \ n ";
- /* Prints
- Peak: 13687072 bytes
- */
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.
- $ Line ){
- // Loop thru each line and prepend line numbers
- Echo "Line # {$ line_num}:". htmlspecialchars ($ line )."
- \ N ";
- }
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.
- Function data_uri ($ file, $ mime ){
- $ Contents = file_get_contents ($ file );
- $ Base64 = base64_encode ($ contents );
- Echo "data: $ mime; base64, $ base64 ";
- }
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.
- Function detect_city ($ ip ){
-
- $ Default = 'unknown ';
-
- 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,
- CURLOPT_HEADER => 0,
- CURLOPT_RETURNTRANSFER => 1,
- CURLOPT_USERAGENT => $ curlopt_useragent,
- CURLOPT_URL => $ url,
- CURLOPT_TIMEOUT => 1,
- CURLOPT_REFERER => 'http: // '. $ _ SERVER ['http _ host'],
- );
-
- Curl_setopt_array ($ ch, $ curl_opt );
-
- $ Content = curl_exec ($ ch );
-
- If (! Is_null ($ curl_info )){
- $ Curl_info = curl_getinfo ($ ch );
- }
-
- Curl_close ($ ch );
-
- If (preg_match ('{
- City: ([^ <] *)
- } I ', $ content, $ regs) {$ city = $ regs [1];} if (preg_match ('{
-
- State/Province: ([^ <] *)
-
- } 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.
- Function get_client_language ($ availableLanguages, $ default = 'en '){
- If (isset ($ _ SERVER ['http _ ACCEPT_LANGUAGE ']) {
- $ Langs = explode (',', $ _ SERVER ['http _ ACCEPT_LANGUAGE ']);
-
- Foreach ($ langs as $ value ){
- $ Choice = substr ($ value, 0, 2 );
- If (in_array ($ choice, $ availableLanguages )){
- Return $ choice;
- }
- }
- }
- Return $ default;
- }
8) Check if server is HTTPS -- Check whether the server is HTTPS
- If ($ _ SERVER ['https']! = "On "){
- Echo "This is not HTTPS ";
- } Else {
- Echo "This is HTTPS ";
- }
9) Generate CSV file from a PHP array--generate a. CSV file in the PHP Group
- Function generateCsv ($ data, $ delimiter = ',', $ enclosure = '"'){
- $ Handle = fopen ('php: // temp ', 'R + ');
- Foreach ($ data as $ line ){
- Fputcsv ($ handle, $ line, $ delimiter, $ enclosure );
- }
- Rewind ($ handle );
- While (! Feof ($ handle )){
- $ Contents. = fread ($ handle, 8192 );
- }
- Fclose ($ handle );
- Return $ contents;
- }
From: Designzum
...