How to better separate PHP files from html code -- eval function usage
Source: Internet
Author: User
How to make php files and html code better separated ------ eval function usage we all know that php is a server-side embedded html-based scripting language. however, if you use embedded html to build a website, the code will soon become huge and uncontrollable. how can we separate php code from html and make a lib (Template) similar to dw to make the page easier to modify and how to better separate php files from html code?
------ Eval function usage
As we all know, php is a server-side embedded html script programming language, but according to the embedded html
The code will soon become huge and uncontrollable. how can we make php code and html
Separate and make a lib (Template) similar to dw so that the page is easier to modify and the code is easy to maintain?
Later, I read a lot of articles, saying that phplib can be implemented. after reading a few pages, I felt dizzy and lost my mind.
Desire to continue (if the prawn has this experience, please don't hesitate to give me some advice. thank you first !). But the problem has to be solved.
After being depressed for many days, I had a chance to download the source code of the vbb Forum.
The html code is rarely seen outside the file. do you think this is the style I want? you can see it. I still feel dizzy :(, the only one
The result is that it stores the html code in the database and calls it through the php file. after a series of processing, it uses the eval function.
Bring the expected variables into the dynamic pages required for generation. in this way, I will not go back to the vbb source code, but transfer it to the eval function.
Starling's php Chinese manual introduces eval functions as follows:
Function: eval ()
Miscellaneous Library
Eval
Enter the value in the string.
Syntax: void eval (string code_str );
Return value: None
Correspondence type: data processing
Description
This function can be used to substitute variable values in a string for processing data in a database. Parameter code_str
Is the string to be processed. It is worth noting that the string to be processed must comply with the PHP string format and end
There must be a semicolon. The strings processed by using this function will be continued to the end of the PHP program.
$ Name is included in $ string.
The cup contains coffee.
The example test has no problems. However, when I test the following code, an error occurs:
$ Aa = 'My name is yyy! ';
$ Str =' ';
Eval ("\ $ str = \" $ str \";");
Echo $ str;
?>
A series of solutions proposed by netizens for help on chainasp
And finally run successfully in this way:
$ Aa = 'My name is yyy! ';
$ Str =' ';
Eval ("\ $ str = \" $ str \";");
Echo $ str;
?>
However, when I insert $ str into the following table and then extract it again, the error occurs again, which is annoying.
Dead.
Database evaltest
# 'Enabtest' table structure'
Create table envtest (
Id tinyint (4) not null auto_increment,
Sour mediumtext,
Primary key (id ),
UNIQUE id (id ),
KEY id_2 (id)
);
# 'Enabtest'
Insert into envtest VALUES ('1 ',' ');
The PHP file is as follows:
$ Aa = 'My name is yyy! ';
$ Conn = mysql_connect ('localhost', 'root ',');
$ Sele = 'SELECT sour from envtest where id = 1 ';
$ Res = mysql_db_query ('evaltest', $ sele );
$ Arra = mysql_fetch_array ($ res );
$ Str = $ arra ['sour'];
Eval ("echo \" $ str \";");
?>
Let's take a look at the Chinese php Manual of Starling and find the following sentence: "The string to be processed must conform to the PHP string format ."
"PHP string format" (anyone knows, please let me know )? I don't know, and I can't find it. I have to look at the string processing function.
I found htmlspecialchars () seemed to be available, so I tried it:
$ Aa = 'My name is yyy! ';
$ Conn = mysql_connect ('localhost', 'root ',');
$ Sele = 'SELECT sour from envtest where id = 1 ';
$ Res = mysql_db_query ('evaltest', $ sele );
$ Arra = mysql_fetch_array ($ res );
$ Str = htmlspecialchars ($ arra ['sour ']);
Eval ("echo \" $ str \";");
?>
However, the page is displayed as follows:
If the variable is successfully imported, the file source code is displayed as follows:
Let's take a look at the htmlspecialchars () usage in the manual and find that this function performs the following operations on the string:
& (And) &
"(Double quotation marks)"
<(Less than) to <
> (Greater than) to>
Search again and find the function that is opposite to this function, so I added several lines of code and made the following debugging, finally succeeded.
Function dehtml ($ str ){
$ Str = str_replace ('"', '"', $ str );
$ Str = str_replace ('<', '<', $ str );
$ Str = str_replace ('>', '>', $ str );
$ Str = str_replace ('&', '&', $ str );
Return $ str;
}
$ Aa = 'My name is yyy! ';
$ Conn = mysql_connect ('localhost', 'root ',');
$ Sele = 'SELECT sour from envtest where id = 1 ';
$ Res = mysql_db_query ('evaltest', $ sele );
$ Arra = mysql_fetch_array ($ res );
$ Str = HTMLSpecialChars ($ arra ['sour ']);
Eval ("echo dehtml (\" $ str \");");
?>
After the code is successfully debugged, I add the source code of a complex html page to a variable and insert it into the evaltest table,
The test is successful again.
In eval function usage, "the string to be processed must conform to the PHP string format ".
The characters processed by the HTMLSpecialChars () function are not correct.
Please test the above methods. if you find any errors or better solutions, please let me know, my email
Address: chensiping@263.net
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.