Dynamic HTML output in PHP _ PHP Tutorial

Source: Internet
Author: User
Dynamic HTML output technology in PHP. You can use echohelloworld anywhere in the php program !; To output the content you want to output. But you will encounter the following troubles: 1-when you try to add two items Between hello and world, you can use them anywhere in the php program.
Echo "hello world! ";
To output the content you want to output.

However, you may encounter the following troubles:

1-
When you try to add two or more spaces Between hello and world,
You use:
Echo "hello world! ";
The output you get is still a space, or you add a space at the beginning of the line,
Your spaces will also be ignored.

2-
Worse, when outputting user input content
It makes your output a mess, and even brings trouble to other users.
For example:



If the user input contains more than one row of content
Echo $ in_txt;
Line breaks are ignored.

3-
In most cases, we do not want users to input html
Code, because you do not know what the user will enter.
Users can even write a piece of code to cause all users on your website to crash.
Of course you don't want that, but if you simply
Echo $ in_txt;
It cannot be avoided.



Solution:
For 1, you can use ereg_replace ("{2}", "$ nbsp;", $ in_txt)
The two spaces are converted into escape characters ($ nbsp ).

For 2, nl2br ($ in_txt) is the best choice, so that the line feed is changed
"
.

For 3, the html code entered by the user is safely displayed, and php also has special functions.
Htmlspecialchars ($ in_txt.


In addition, if $ in_txt is proposed from the mysql database
Make sure to use addslashes (). correspondingly, stripslashes () must be used for retrieval ().

Summary:
If $ in_txt is the text entered by the user, it can be output as follows:
Echo ereg_replace ("{2}", "", nl2br (htmlspecialchars (stripslashes ($ in_txt ))));

Echo hello world !; To output the content you want to output. But you will encounter the following troubles: 1-when you try to add two...

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.