PHP Face Question Collection 2

Source: Internet
Author: User
Tags echo date flock

1, the realization of Chinese character string interception without garbled method

Open the mbstring extension, and then customize the function:

<?phpheader (' content-type:text/html:charset=utf-8 '); function Substr_utf8 ($str, $start, $length = null) {return Join ("", Array_slice (Preg_split ("//u", $str,-1, Preg_split_no_empty), $start, $length));} Example $STR = "I am a good boy!"; Echo Substr_utf8 ($str, 2, 4);

2. Print the day before using PHP

<?phpheader (' content-type:text/html:charset=utf-8 '); Echo date (' y-m-d h:i:s ', Strtotime ('-1 day '));

3, does not apply the third variable Exchange 2 variables value

<?phpheader (' content-type:text/html:charset=utf-8 '); $a = ' a '; $b = ' B '; list ($a, $b) = Array ($b, $a); Echo $a, $b;

4. Convert 1234567890 to 1,234,567,890

Header (' Content-type:text/html:charset=utf-8 '); $str = ' 1234567890 ';//invert string $str = Strrev ($STR);//Comma separated by 098, 765,432,1, $str = Chunk_split ($str, 3, ', ');//Invert $str = Strrev ($STR);//Remove Left, $str = LTrim ($str, ', '); Echo $str;

5. Implement UTF8 string inversion

Cannot use Strrev, Chinese will be wrong

function Strrev_utf8 ($STR) {return join ("", Array_reverse (Preg_split ("//u", $str)));} $str = "I am a good boy"; Echo Strrev_utf8 ($STR);

6, take the URL file extension, as much as possible to implement the method

$str = "www.baidu.com/index.php"; function Get_ext1 ($STR) {return strrchr ($str, '. ');} function Get_ext2 ($str) {return substr ($str, Strrpos ($str, '. '));} function Get_ext3 ($str) {$str = PathInfo ($STR); return $str [' extension '];} function Get_ext4 ($str) {$arr = explode ('. ', $str); return $arr [Count ($arr)-1];} function Get_ext5 ($str) {$pattern = '/^[^\.] +\. ([\w]+) $/'; return preg_replace ($pattern, ' ${1} ', basename ($STR));}

7. Write a function to convert the string Open_door to Opendoor

$str = "Open_door", function Change_str ($str) {$arr = explode (' _ ', $str); $arr = Array_map (' Ucfirst ', $arr); return implode ( ", $arr);} echo Change_str ($STR);

8. Single case mode

<?phpclass mysql{private static $instance = Null;private $conn;//set to private, do not allow object private function through new __construct () {$ conn = mysql_connect (' localhost ', ' root ', ' 123456 ');} Gets the instance method public static function getinstance () {if (! Self:: $instance instanceof Self) {self:: $instance = new Self;} Return self:: $instance;} Disallow cloning of private function __clone () {}} $db = Mysql::getinstance ();

9. Write a PHP code to ensure that multiple processes write to the same file successfully

<?PHP$FP = fopen ("Lock.txt", "w+"), if (Flock ($FP, LOCK_EX)) {//Get write lock fwrite ($fp, ' write something '); Flock ($FP, Lock_un );} Else{echo "File is locking ...";} Fclose ($FP);

10. Get the file name extension from a completed URL

<?php$url = ' http://www.baidu.com/a/b/index.php?id=1 '; $arr = Parse_url ($url); $fname = basename ($arr [' path ']); $arr = Explode ('. ', $fname); Echo $arr [Count ($arr)-1];

11. Write a function to facilitate all files and subfolders under a folder

<?phpfunction My_scandir ($dir) {$files = array (), if (Is_dir ($dir)) {if ($handle = Opendir ($dir)) {while ($file = Readdir ($handle))!== false) {if ($file! = "." && $file! = "...") {if (Is_dir ($dir. '/'. $file)) {$files [$file] = My_scandir ($dir. '/'. $file);} else{$files [] = $dir. '/'. $file;}}} Closedir ($handle); return $files;}}} Var_dump (My_scandir (' D:\wamp\www\study '));

12. The principle of unlimited classification in the forum

First design the database table

CREATE TABLE category (cate_id int unsigned not NULL auto_increment primary key,cat_name varchar (in) NOT null default ', PA rent_id int unsigned NOT NULL default 0) Engine=innodb Charset=utf8;

Then use the function to recursively implement, infinite classification

function tree ($arr, $pid =0, $level =0) {Static $list = array (); foreach ($arr as $v) {///if it is a top-level classification, the $list//is then placed at this node as the root, Traverse its child nodes if ($v [' parent_id '] = = $pid) {$v [' level '] = $level; $list [] = $v; tree ($arr, $v [' cat_id '], $level + 1)}} return $list;}

13, calculate the relative path of 2 files

<?php$a = '/a/b/c/d/a.php '; $b = '/a/b/e/f/b.php '; $arr 1 = explode ('/', dirname ($a)), $arr 2 = explode ('/', dirname ($b)); For ($i =0, $len =count ($arr 2), $i < $len, $i + +) {if ($arr 1[$i]! = $arr 2[$i]) {break;}} Not in a root directory if ($i = = 1) {$ret = Array ();} Under the same root directory if ($i! = 1 && $i < $len) {$ret = Array_fill (0, $len-$i, "..");} Under the same directory if ($i = = $len) {$ret = Array ('./');} $ret = Array_merge ($ret, Array_slice ($arr 1, $i)); Echo implode ('/', $ret);

14. Joseph Ring Question

<?phpfunction King ($n, $m) {$monkey = range (1, $n), $i = 0;while (count ($monkey) > 1) {$i + = 1; $head = Array_shift ($monke y);//To the front of the first if ($i% $m! = 0) {//If it is not a multiple of M, then return to the tail, otherwise the Array_push ($monkey, $head);}} return $monkey [0];} echo King (10,7);

15, PHP implementation of two-way queue

<?phpclass dqueue{private $queue = Array ();p ublic function AddFirst ($item) {return Array_unshift ($this->queue,$ Item);} Public Function AddLast ($item) {return Array_push ($this->queue, $item);} Public Function GetFirst () {return array_shift ($this->queue);} Public Function GetLast () {return Array_pop ($this->queue);}}

PHP Face Question Collection 2

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.