search
HomeBackend DevelopmentPHP TutorialBasic usage of php built-in functions

Basic usage of php built-in functions

Jun 28, 2017 am 10:25 AM
phpbuilt-in functions

In fact, these contents can be learned in the php manual, but many php beginners do not know how to read the php manual and cannot understand it

So Aiwei.com will now explain the built-in functions of php
There are functions related to case conversion
Text html tag processing function
Case related functions

The code is as follows:

strtolower() 
strtoupper() 
ucfirst() 
ucword()

HTML tag related The string formatting function

code is as follows:

nl2br() 
htmllentities() 
htmlspecialchars
() 
stripslashes
() 
strip_tags() 
number_format
() 
strrev() 
md5()

All string processing functions in php do not modify the original string, but return a newly formatted The string

code is as follows:

<?php 
//转换成小写 
$a=&#39;www.jb51.net&#39;; 
echo strtolower($a); 
//结果:www.jb51.net 
//转换成大写 
$a=&#39;www.jb51.net&#39;; 
echo strtoupper($a); 
//结果:WWW.jb51.net 
//首字母大写 
$a=&#39;www.jb51.net&#39;; 
echo ucfirst($a); 
//结果:Www.jb51.net 
//每个单词首字母大写 
$a=&#39;i love you&#39;; 
echo ucword($a); 
//结果:I Love You 
/* 
提示:大家都知道大小写,认为小写和大写有区别吗,但是为什么要区分大小写呢 
在win系统下php大小写不严格 但是在linux系统下就严禁拉 大小写不能乱写 
比如 
在
自动加载类
的时候 
<?php 
function _autoload($className){ 
include strtolower($className).&#39;.class.php&#39;; 
} 
$obj= new MyClass; 
这样就加载myclass.class.php 
因为文件名常是小写那么必须转换小写 
?> 
*/ 
//nl2br把空格转换成实体<br/>因为一般在浏览器里显示的换行都是<br/> 例如在表单留言本里必须要转换不然折行不成功 再多的空格都任务是一个空格 
$a=&#39; 
i 
love 
you 
&#39;; 
echo $a; 
echo nl2br($a); 
结果1:i love you 
结果2: 
i 
love 
you 
//表单提交如果你不进行html标签处理那么就会直接显示样式或者js代码直接运行 
/* 
<form> 
<input type=&#39;text&#39; name=&#39;title&#39;> 
<input type=&#39;submit&#39; name=&#39;submit&#39; value=&#39;提交&#39;> 
</form> 
当你输入 
<H1>www.jb51.net<H1> 
一提交就出现是一号大字体 
但是你原来是想要 
<H1>www.jb51.net<H1>结果的 
所以要处理下 
当输入<script>alert(&#39;www.jb51.net&#39;)</script> 
一提交就会运行javascript 
这样不好 必须处理下来防止黑客找到你攻击的入口 
表单默认提交方式是get 
*/ 
//当你输入<p><h1 id="www-jb-net">www.jb51.net</h1></p> 
echo htmlspecialchars($_GET[&#39;title&#39;]);//过滤了 
结果:<p><h1 id="www-jb-net">www.jb51.net</h1></p> 
其他查看源码就知道< 和 >已经被替换了成&lt &gt就会在页面原型显示 
还有一点要主要 如果不处理<p >有的复制的文章自身有标签样式就会打乱你的
页面布局
 可能css冲突 
htmllentities()函数用户和htmlspecialchars()相反用法就不说了 
当你需要那个标签留着可以用到strip_tags()函数 
echo strip_tags($_GET[&#39;title&#39;],&#39;<b><p><strong><h1>&#39;); 
提交结果是你查看源码 就会发现<p>没有了 
/* 
加入输入i love &#39;jb51&#39;; 
提交结果为 i love \&#39;jb51\&#39;反斜杠转义了 
那么我要想原文输出怎么办呢 
可以用这个php函数stripslashes() 
取消转义 
echo stripslashes($_GET[&#39;title&#39;]); 
结果是i love &#39;jb51&#39;; 
如果含有html标签呢如这种 
<a href="http://www.jb51.net/">i love &#39;jb51&#39;</a> 
我要原型输出怎么办 可以用2个函数结合起来用 我已经说过的 
echo htmlspecialchars(stripslashes($_GET[&#39;title&#39;])); 
结果:<a href="http://www.jb51.net/">i love &#39;jb51&#39;</a> 
*/ 
//number_format()这个函数是格式化货币函数 不同国家的习惯不一样那么需要的货币显示就不一样例如商城中国钱是通常是这样的格式 
千分位分割用逗号 保留几位用点 人称‘小数点&#39; 
这个函数的用法很简单 
number_format($money,小数点保留几位,&#39;小数点用什么分开&#39;,&#39;千分位用什么分开&#39;) 
$price=&#39;123465789.233&#39;; 
echo number_format($money,2,&#39;,&#39;,&#39;.&#39;); 
结果:123.465.789,23 
echo number_format($money,2,&#39;.&#39;,&#39;,&#39;);//中国式的 
结果:123,465,789.23 
//strrev()使字符串反倒过来 
$str=&#39;http://www.jb51.net&#39;; 
echo strrev($str); 
结果:moc.tenwii.www//:ptth 
//md5就是加密 用户名密码必须要加密防止黑客 
$a=&#39;admin&#39;; 
echo $b= md5($a);


The above is the detailed content of Basic usage of php built-in functions. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

How do you redirect a page in PHP?How do you redirect a page in PHP?Apr 28, 2025 pm 04:54 PM

The article discusses various methods for page redirection in PHP, focusing on the header() function and addressing common issues like "headers already sent" errors.

What is PDO in PHP?What is PDO in PHP?Apr 28, 2025 pm 04:51 PM

The article discusses PHP Data Objects (PDO), an extension for database access in PHP. It highlights PDO's role in enhancing security through prepared statements and its benefits over MySQLi, including database abstraction and better error handling.

What is Memcache and Memcached in PHP? Is it possible to share a single instance of a Memcache between several projects of PHP?What is Memcache and Memcached in PHP? Is it possible to share a single instance of a Memcache between several projects of PHP?Apr 28, 2025 pm 04:47 PM

Memcache and Memcached are PHP caching systems that speed up web apps by reducing database load. A single instance can be shared among projects with careful key management.

What are the steps to create a new database using MySQL and PHP?What are the steps to create a new database using MySQL and PHP?Apr 28, 2025 pm 04:44 PM

Article discusses steps to create and manage MySQL databases using PHP, focusing on connection, creation, common errors, and security measures.

Does JavaScript interact with PHP?Does JavaScript interact with PHP?Apr 28, 2025 pm 04:43 PM

The article discusses how JavaScript and PHP interact indirectly through HTTP requests due to their different environments. It covers methods for sending data from JavaScript to PHP and highlights security considerations like data validation and prot

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor