Home 类库下载 PHP类库 PHP string processing built-in functions

PHP string processing built-in functions

Oct 21, 2016 am 10:59 AM
php function string

PHP built-in functions:


addcslashes — Add backslash escape characters to some characters in the string
addslashes — Escape characters in the string in a specified way
bin2hex — Convert binary data into Hexadecimal representation
chop — Alias ​​function of rtrim()
chr — Returns the ASCII code of a character
chunk_split — Splits a string into small pieces according to a certain character length
convert_cyr_string — Converts Cyrillic characters to other characters
convert_uudecode — Decrypt a string
convert_uuencode — Encrypt a string
count_chars — Return character usage information in a string
crc32 — Calculate the crc32 polynomial of a string
crypt — One-way hash encryption function
echo — Used to Display some content
fprintf — Return the data as required and write it directly to the document stream
get_html_translation_table — Return an HTML entity that can be converted
hebrev — Convert a Hebrew-encoded string to visual text
hebrevc — Convert Hebrew-encoded Convert a string into visual text
html_entity_decode — The inverse function of the htmlentities () function, convert HTML entities into characters
htmlentities — Convert some characters in the string into HTML entities
htmlspecialchars_decode — The inverse function of htmlspecialchars() function, convert Convert HTML entities to characters
htmlspecialchars — Convert some characters in a string to HTML entities

explode — Convert a string into an array using delimiters

implode — Convert an array into a string using a specific delimiter
join — Convert an array into a string, alias of the implode() function
levenshtein — Calculate the difference between two words
localeconv — Get number-related format definitions
ltrim — Remove the blanks or specified characters on the left side of the string
md5_file — Encrypt a file with the MD5 algorithm
md5 — Encrypt a string with the MD5 algorithm
metaphone — Determine the pronunciation rules of a string
money_format — According to the parameters Formatted output of numbers
nl_langinfo — Query language and local information
nl2br — Replace the newline character "n" in the string with "
"
number_format — Formatted output of numbers according to parameters
ord — Replace an ASCII Convert the code into a character
parse_str — Convert a string in a certain format into a variable and value
print — Used to output a single value
printf — Display the data as required
quoted_printable_decode — Encrypt a string into an 8-bit Binary string
quotemeta — Escape several specific characters
rtrim — Remove the blanks or specified characters on the right side of the string
setlocale — Set local formats for numbers, dates, etc.
sha1_file — SHA1 a file Algorithm encryption
sha1 — Encrypt a string with SHA1 algorithm
similar_text — Compare two strings and return the number of similar characters considered by the system
soundex — Determine the pronunciation rules of a string
sprintf — Return the data as required, But it does not output
sscanf — can format strings
str_ireplace — matches and replaces strings like the str_replace() function, but is not case-sensitive
str_pad — pads both sides of the string
str_repeat — strings Perform repeated combinations
str_replace — Match and replace strings
str_rot13 — Encrypt the string with ROT13
str_shuffle — Randomly sort the characters in a string
str_split — Split a string into an array according to the character spacing
str_word_count — Obtain English word information in a string
strcasecmp — Compare strings in size, case-insensitively
strchr — Return part of a string through comparison An alias of the strstr() function
strcmp — Compare strings in size
strcoll - Compare the size of strings according to local settings
strcspn - Return the value of the continuous non-matching length of characters
strip_tags - Remove the HTML and PHP code in a string
stripcslashes - Unescap the addcslashes() function escape processing String
stripos — Find and return the position of the first match, matching is case-insensitive
stripslashes — Unescaped addslashes() function escapes the processed string
stristr — Returns parts of a string by comparison, comparison Case-insensitive
strlen — Get the encoded length of a string
strnatcasecmp — Use natural sorting to compare strings, case-insensitive
strnatcmp — Use natural sorting to compare strings
strncasecmp — Right Compare the first N characters of a string, case-insensitive
strncmp — Compare the first N characters of a string
strpbrk — Return a part of a string by comparison
strpos — Find and return the first match The position
strrchr — Returns the part of a string by comparing from back to front
strrev — Arranges all the letters in the string in reverse order
strripos — Searches from back to front and returns the position of the first match, matches are indistinguishable Uppercase and lowercase
strrpos - Search from back to front and return the position of the first match
strspn - Match and return the value of the length of consecutive occurrences of characters
strstr - Return a part of a string by comparison
strtok - Use a specified number of characters To split the string
strtolower — Convert the string to lowercase
strtoupper — Convert the string to uppercase
strtr — Compare and replace the string
substr_compare — Compare the truncated string
substr_count — Count a certain character in the string The number of occurrences of the segment
substr_replace — Replace some characters in the string
substr — Truncate the string
trim — Remove the blanks or specified characters on both sides of the string
ucfirst — Replace the first letter of the given string Convert to uppercase
ucwords — Convert the first letter of each English word in the given string to uppercase
vfprintf — Return the data as required and write it directly to the document stream
vprintf — Display the data as required
vsprintf - Return data as required, but do not output
wordwrap - Split the string according to a certain character length


Custom function:


1, the function range() to quickly create an array

For example The range() function can quickly create a number array from 1 to 9:

<?php 
    $numbers=range(1,9); //用range直接创建1~9共9个数字组成的数组,以“1”开始“9”结束。 
    echo $numbers[1]; //输出创建的第二个数组值:2; echo $numbers[0];则输入第一个值:0。 
?>
Copy after login

Of course, using range(9,1) creates a number array from 9 to 1. At the same time, range() can also create a character array from a to z:

<?php 
    $numbers=range(a,z); 
    foreach ($numbers as $mychrs) //遍历$numbers数组,每次循环当前单元值被赋给$mychrs 
    echo $mychrs." "; //output a b c d e f g h i j k l m n o p q r s t u v w x y z 
?>
Copy after login

//foreach是一种遍历数组的简便方法,foreach 仅能用于数组,当试图将其用于其它数据类型或者一个未初始化的变量时会产生错误,它有两种格式:

foreach (array_expression as $value) statementforeach (array_expression as $key => $value) statement
第一种格式遍历给定的 array_expression 数组。每次循环中,当前单元的值被赋给 $value 并且数组内部的指针向前移一步(因此下一次循环中将会得到下一个单元)。第二种格式做同样的事,只除了当前单元的键名也会在每次循环中被赋给变量 $key


使用字符数组时注意大小写,比如range(A,z)和range(a,Z)是不一样的。

range()函数还具有第三个参数,该参数的作用是设定步长,比如range(1,9,3)创建的数组元素是:1、4、7

2,PHP中常规数组的排序

一般数组中的各元素均以字符或数字表现的,所以可对数组元素进行升序排列,该功能函数为sort()。比如:

<?php 
    $people=array(&#39;name&#39;,&#39;sex&#39;,&#39;nation&#39;,&#39;birth&#39;); 
    foreach ($people as $mychrs) 
        echo $mychrs." "; 
    sort($people); 
    echo "---排序后---"; 
    foreach ($people as $mychrs) 
        echo $mychrs." "; 
?>
Copy after login

升序排序后的数组元素显示为 birth name nation sex,当然,sort()函数是区分字母大小写的(字母从大到小的顺序是:A…Z…a…z)

Sort()函数还具有第二参数,用来说明升序的规则是用来比较数字还是字符串的。比如:

<?php 
    echo "---按数字升序排序---
    "; 
    $num2=array(&#39;26&#39;,&#39;3&#39;,); 
    sort($num2,SORT_NUMERIC); 
    foreach ($num2 as $mychrs) 
    echo $mychrs." "; 
    echo "
    ---按字符升序排序---
    "; 
    $num3=array(&#39;26&#39;,&#39;3&#39;); 
    sort($num3,SORT_STRING); 
    foreach ($num3 as $mychrs) 
    echo $mychrs." "; 
?>
Copy after login

SORT_NUMERIC和SORT_STRING用来声明按数字或字符的升序排列。如果按照数字升序排列是:3,26;但如果按照字符升序排列则是:26,3了。

PHP中除了升序函数以外,还有降序或称反向排列的函数,就是rsort()函数,比如:

$num1=range(1,9);
rsort($num1); //这里其实就相当于range(9,1)

3,PHP中关联数组的排序

PHP除了支持数字索引数组以外,还支持相关数组。比如如下数组就是一个相关(关联)数组:

$peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth');

使用sort($peoples)默认即是按照元素定义值的升序排序,在关联数组中可使用asort()函数表示按元素值升序排序,关联数组中最主要的则是可按照关键字(如xm、xb、mz等)的升序排序,该方法是用函数ksort()函数。

<?php 
    $peoples=array(&#39;xm&#39;=>&#39;name&#39;,&#39;xb&#39;=>&#39;sex&#39;,&#39;mz&#39;=>&#39;nation&#39;,&#39;cs&#39;=>&#39;birth&#39;); 
    foreach ($peoples as $mychrs) 
    echo $mychrs." "; 
    echo "
    --按元素值升序排列--
    "; 
    asort($peoples); 
    foreach ($peoples as $mychrs) 
    echo $mychrs." "; 
    echo "
    --按关键字升序排列--
    "; 
    ksort($peoples); 
    foreach ($peoples as $mychrs) 
    echo $mychrs." "; 
?>
Copy after login

和常规数组拥有sort()升序函数的反向排序rsort()降序函数相对应的,关联数组也有对应的降序函数:asort()函数和arsort()函数、ksort()函数和krsort()函数。
记忆:原型函数是sort(),其中a、k表示关联数组相关必须前置,反向排序使用r修饰。

4,PHP数组元素随机排序

PHP中使用shuffle()函数将数组元素进行随机的重新排序,每次都会显示不同的排序组合,比如:

<?php 
    $fer=array(&#39;cnbruce&#39;,&#39;cnrose&#39;,&#39;cnjames&#39;,&#39;cnanne&#39;); 
    shuffle($fer); //随即排序,每刷新一次页面则进行一次随机排序。 
    foreach ($fer as $mychrs) 
    echo $mychrs." "; 
?>
Copy after login

5,PHP数组按原顺序反向排序

PHP中可使用array_reverse()函数将数组元素按原顺序反向排序。比如:

<?php 
$fer=array(&#39;cnbruce&#39;,&#39;cnrose&#39;,&#39;cnjames&#39;,&#39;cnanne&#39;); 
foreach ($fer as $mychrs) 
echo $mychrs." "; 
$fer=array_reverse($fer); //将数组内元素按原顺序反向排序 
echo "--按原顺序反向--"; 
foreach ($fer as $mychrs) 
echo $mychrs." "; 
?>
Copy after login


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

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

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.

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

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.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles