PHP Sina interview questions all questions and answers

Source: Internet
Author: User
Tags explode file copy php language php regular expression

1. Write a function to retrieve the file extension from a standard url as efficiently as possible.
For example:/abc/de/fg. php? Id = 1 need to retrieve php or. php

A: I did this directly using PHP built-in functions without repeating the wheel. It is estimated that the author also wants to evaluate the basic knowledge, mainly to parse the url and a function that returns the file information (extension: multiple methods for getting the file suffix ):

The code is as follows: Copy code

<? Php
/** */
$ Url = "/abc/de/fg. php? Id = 1 ";
$ Path = parse_url ($ url );
Echo pathinfo ($ path ['path'], PATHINFO_EXTENSION); // php
?>


2. In HTML, the meta tag in the page header can be used to output the file encoding format. The following is a standard meta statement.
<META http-equiv = 'content-type' Content = 'text/html; charset = gbk'>
Use the PHP language to write a function and change the charset part of a standard HTML page similar to meta tag to big5.
Note:
(1) the complete html page must be processed, that is, not only this meta statement
(2) case insensitive
(3) 'and "here are interchangeable
(4) the quotation marks on both sides of 'content-type' can be ignored, but the quotation marks on both sides of 'text/html; charset = gbk' cannot.
(5) handle extra spaces

A: I forgot the regular expression (PHP regular expression details) for about half a day.

The code is as follows: Copy code

<? Php
/***/
$ Html = "<meta http-equiv = 'content-type' Content = 'text/html; charset = gbk'> ";
// Match the standard meta tag
$ Pattern = "/<metas + http-equiv = ('| ")? Content-Type ('| ")? S + content = ('| ") text/html; s + charset = (. *) (' |")>/I ";
$ Replacement = "<meta http-equiv = 'content-type' Content = 'text/html; charset = big5 '> ";
$ Result = preg_replace ($ pattern, $ replacement, $ html );
Echo htmlspecialchars ($ result );
?>

3. Write a function to calculate the relative path of the two files.
For example, $ a = '/a/B/c/d/e. Php ';
$ B = '/a/B/12/34/c. Php ';
Calculate the relative path of $ B relative to $ a./../c/d to add ().

Answer:

The code is as follows: Copy code

<? Php
/** */
$ A = '/a/B/c/d/e. Php ';
$ B = '/a/B/13/34/c. Php ';
Echo getRelativePath ($ a, $ B); // "..././12/34 /"
Function getRelativePath ($ a, $ B ){
$ A2array = explode ('/', $ );
$ B2array = explode ('/', $ B );
$ RelativePath = '';
For ($ I = 1; $ I <= count ($ b2array)-2; $ I ++ ){
$ RelativePath. = $ a2array [$ I] ==$ b2array [$ I]? '../': $ B2array [$ I]. '/';
        }
Return $ relativePath;
    }
?>


4. Write a function to traverse all files and subfolders in a folder.

A: I wrote this in my blog (php file traversal and file copy), but there are many implementation methods and the efficiency is not necessarily the highest.

The code is as follows: Copy code


/*
* @ Blog
*/
Function listDir ($ dir = '.'){
If ($ handle = opendir ($ dir )){
While (false! ==( $ File = readdir ($ handle ))){
If ($ file = '.' | $ file = '..'){
Continue;
   }
If (is_dir ($ sub_dir = realpath ($ dir. '/'. $ file ))){
Echo 'File in PATH: '. $ dir.': '. $ FILE.' <br> ';
ListDir ($ sub_dir );
} Else {
Echo 'File: '. $ FILE.' <br> ';
   }
  }
Closedir ($ handle );
 }
}
 
ListDir ('E: wwwabc ');


5. Briefly describe the implementation principles of unlimited classification in the forum.

A: The Infinitus classification should be a recursive function!

Step 1: Create a test database:

The code is as follows: Copy code

Create table 'Category '(
'Id' smallint (5) unsigned not null auto_increment,
'Fid' smallint (5) unsigned not null default '0 ',
'Value' varchar (50) not null default '',
Primary key ('id ')
) ENGINE = MyISAM default charset = utf8;


Step 2: insert test data:

The code is as follows: Copy code

Insert into 'category '('fid', 'value') VALUES
(0, 'php point-to-point blog Pxx. Com '),
(1, 'A '),
(1, 'B '),
(2, 'C '),
(2, 'D '),
(4, 'E ')


Step 3: recursive output classification:

The code is as follows: Copy code


<? Php
/** */
$ Conn = mysql_connect ("localhost", "root", "mckee ");
Mysql_select_db ("test", $ conn );
Mysql_query ("set names utf8 ");
$ SQL = "SELECT * FROM category ";
$ Res = mysql_query ($ SQL );
While ($ row = mysql_fetch_assoc ($ res )){
$ Arr [] = array ($ row [id], $ row [fid], $ row [value]);
}
GetCate (0 );
Function getCate ($ fid = 0 ){
Global $ arr;
For ($ I = 0; $ I <count ($ arr); $ I ++ ){
If ($ arr [$ I] [1] ==$ fid ){
Echo $ arr [$ I] [2]. "<br> ";
GetCate ($ arr [$ I] [0]); // recursion
        }
    }
}
?>

6. Design a webpage so that a full screen window is displayed when it is opened, which contains a text box and a button. After entering information in the text box, you can click the button to close the window. The entered information is displayed on the main page!

Answer: Nima. I don't know what the problem is. Have Sina engineers gotten into the water? Evaluate the knowledge of js window objects? Tell me?

Index.html

The code is as follows: Copy code

<Html>
<Head>
<Title> by </title>
</Head>
<Body>
<H1> <Script type = "text/javascript">
Open('fullwin.html ');
</Script>
</Body>
</Html>


Fullwin.html

The code is as follows: Copy code


<Html>
<Head>
<Title> by </title>
</Head>
<Body>
<Script type = "text/javascript">
Window. moveTo (0, 0 );
Window. resizeTo (window. screen. width, window. screen. height );
Var s = prompt ('Enter :');
Optional values opener.doc ument. getElementsByTagName ('h1 ') [0]. innerText = s;
Window. close ();
</Script>
</Body>
</Html>


Unset reference

The code is as follows: Copy code

<? Php
$ A = "this is a php blog ";
$ B = & $;
Unset ($ B );
Echo $ a; // this is a php blog
?>

As I know, unset ($ B) only disconnects the variable name and value binding, but the magic is:

 

The code is as follows: Copy code
<? Php
$ A = "test ";
$ B = & $;
Unset ($ );
Echo $ B; // test
?>

But I destroyed $ a. Why is the value of $ B still there? Dizzy, solve it!

Thank you for your reply:

Note about unset () function usage: The address will be released only when all variables pointing to this value (for example, any referenced variable pointing to this value) are destroyed, as shown below:

 

The code is as follows: Copy code
<? Php
/** By www.111cn.net */
$ A = "test ";
$ B = & $;
Unset ($ );
Unset ($ B );
Echo $ B; // The output is empty.
?>
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.