Summarize 19 super useful PHP code snippets
This article mainly explains "summing up 19 super-practical PHP code fragments". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "summarize 19 super-practical PHP code fragments".
1) Whois query using PHP-- use PHP to obtain Whois requests
With this code, whois information can be obtained in a specific domain name. Take the domain name as a parameter and display information about all domain names.
The copy code is as follows:
Function whois_query ($domain) {
/ / fix the domain name:
$domain = strtolower (trim ($domain))
$domain = preg_replace ('/ ^ http:\ /\ / iTunes,'', $domain)
$domain = preg_replace ('/ ^ www\. / iTunes,', $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
/ /
$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.to"
"tv" = > "whois.tv"
"ru" = > "whois.ripn.net"
"org" = > "whois.pir.org"
"aero" = > "whois.information.aero"
"nl" = > "whois.domain-registry.nl"
);
If (! isset ($servers [$ext])) {
Die ('Error: No matching nic server roommates')
}
$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: Could not connect to'. $nic_server. '!);}
Return $output
}
2) Text messaging with PHP using the TextMagic API-use TextMagic API to obtain PHP Test information
TextMagic introduces a powerful core API that can easily send SMS to your mobile phone. The API is subject to payment.
The copy code is as follows:
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 '999123-4567'
$result = $router- > send ('Wake upbringing, 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-- to obtain memory utilization
This code helps you get memory usage.
The copy code is as follows:
Echo "Initial:" .memory _ get_usage (). "bytes\ n"
/ * prints
Initial: 361400 bytes
, /
/ / let's use up some memory
For ($I = 0; $I < 1000000; $iTunes +) {
$array [] = md5 ($I)
}
/ / let's remove half of the array
For ($I = 0; $I < 1000000; $iTunes +) {
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-- to view the source code of any web page
If you want to view the source code of the web page, just change the URL on the second line and the source code will be displayed on the web page.
The copy code is as follows: