Home php教程 php手册 巧学巧用:如何使用PHP中的字符串函数

巧学巧用:如何使用PHP中的字符串函数

Jun 21, 2016 am 08:59 AM
explode implode strtolower strtoupper substr

  (1)echo,print,printf,sprintf

前两个函数是输出字符串.字符串中如果有变量名则被替换成其值.

后两个函数类似于C的同名函数.

(2)strchr,strlen,strtok,strrchr,strrev,strstr,strtolower,

strtoupper,substr,ucfirst

这些是常用的字符串操作函数,有些和C中的同名函数意义完全一致.

strrev是把一个字符串翻转.

strtolower和strtoupper的意思应该不用解释了.

ucfirst是把字符串的第一个字符变成大写.

substr是返回字符串的一个子串,用法是:substr(字符串,头,长度).

头位置是 从0算起的.如果是负数,则是从尾部向前数的意思.

(3)Chr,Ord

类似于C的同名函数.

(4)explode,implode,join

这些是和数组有关的函数.

explode(字符串,分割符)返回一个将字符串在分割符处分开所产生的数组.

implode(数组,分割符)返回一个将数组各元素之间插上分割符而成的字符串.

join与implode意义相同.

(5)Chop

去掉字符串尾部的空白.

(6)htmlspecialchars

将字符串中的HTML特殊字符换成它们的名字,例如\"<\"变成\"<\".

(7)nl2br

在字符串中的每一个回车前面加上\"<BR>\".

(8)AddSlashes,StripSlashes

分别给字符串中需要加上\"\\"才能用于数据库查询的字符加上和去掉\"\\".

(9)parse_str

将\"name1=value1&name2=value2&...\"类型的字符串分析成一些变量.

例如:
parse_str(\"a=1&b=2\");

生成$a与$b两个变量,值分别为1,2.

如果有两对名字/值的名字部分相同,则后一个的值覆盖前一个的.

如果这两对的名字尾部都有\"[]\",例如\"a[]=1&a[]=2\",则生成数组$a,两个元素分别为1,2



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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Convert array to string using implode() function in PHP Convert array to string using implode() function in PHP Jun 26, 2023 pm 11:51 PM

In PHP, we often need to combine elements in an array into a string. At this time, the implode() function comes in handy. The implode() function converts the elements in the array into strings and concatenates them to produce a complete string. This article will introduce how to use the implode() function in PHP to convert an array into a string. 1. Basic usage of implode() function The implode() function has two parameters. The first parameter

Convert string to lowercase using PHP function 'strtolower' Convert string to lowercase using PHP function 'strtolower' Jul 24, 2023 pm 12:34 PM

Convert a string to lowercase using PHP function "strtolower" In PHP, there are many functions that can be used to convert the case of a string. One of the very commonly used functions is strtolower(). This function converts all characters in a string to lowercase. Here is a simple example code showing how to use the strtolower() function to convert a string to lowercase: <?php//original string $string="

How to use PHP explode function and solve errors How to use PHP explode function and solve errors Mar 10, 2024 am 09:18 AM

The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

Common errors and solutions when using the explode function in PHP Common errors and solutions when using the explode function in PHP Mar 11, 2024 am 08:33 AM

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

PHP Warning: implode(): Invalid arguments passed solution PHP Warning: implode(): Invalid arguments passed solution Jun 23, 2023 am 09:04 AM

In PHP programming, Warning messages often appear. One of the more common prompts is "PHPWarning:implode():Invalidargumentspassed". This article will introduce the ideas and methods to solve this Warning. First, we need to understand how to use the implode() function. The function of this function is to concatenate an array into a string, using the following syntax: string

PHP returns the ASCII value of the first character of the string PHP returns the ASCII value of the first character of the string Mar 21, 2024 am 11:01 AM

This article will explain in detail the ASCII value of the first character of the string returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the ASCII value of the first character of a string Introduction In PHP, getting the ASCII value of the first character of a string is a common operation that involves basic knowledge of string processing and character encoding. ASCII values ​​are used to represent the numeric value of characters in computer systems and are critical for character comparison, data transmission and storage. The process of getting the ASCII value of the first character of a string involves the following steps: Get String: Determine the string for which you want to get the ASCII value. It can be a variable or a string constant

PHP returns the string from the start position to the end position of a string in another string PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

Split and merge strings using the explode and implode functions Split and merge strings using the explode and implode functions Jun 15, 2023 pm 08:42 PM

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra

See all articles