Home Backend Development PHP Tutorial Basic usage of php built-in functions

Basic usage of php built-in functions

Jun 28, 2017 am 10:25 AM
php built-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()
Copy after login

HTML tag related The string formatting function

code is as follows:

nl2br() 
htmllentities() 
htmlspecialchars
() 
stripslashes
() 
strip_tags() 
number_format
() 
strrev() 
md5()
Copy after login

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>www.jb51.net</h1></p> 
echo htmlspecialchars($_GET[&#39;title&#39;]);//过滤了 
结果:<p><h1>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);
Copy after login


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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles