国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home 類(lèi)庫(kù)下載 PHP類(lèi)庫(kù) 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直接創(chuàng)建1~9共9個(gè)數(shù)字組成的數(shù)組,以“1”開(kāi)始“9”結(jié)束。 
    echo $numbers[1]; //輸出創(chuàng)建的第二個(gè)數(shù)組值:2; echo $numbers[0];則輸入第一個(gè)值:0。 
?>

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數(shù)組,每次循環(huán)當(dāng)前單元值被賦給$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 
?>

//foreach是一種遍歷數(shù)組的簡(jiǎn)便方法,foreach 僅能用于數(shù)組,當(dāng)試圖將其用于其它數(shù)據(jù)類(lèi)型或者一個(gè)未初始化的變量時(shí)會(huì)產(chǎn)生錯(cuò)誤,它有兩種格式:

foreach (array_expression as $value) statementforeach (array_expression as $key => $value) statement
第一種格式遍歷給定的 array_expression 數(shù)組。每次循環(huán)中,當(dāng)前單元的值被賦給 $value 并且數(shù)組內(nèi)部的指針向前移一步(因此下一次循環(huán)中將會(huì)得到下一個(gè)單元)。第二種格式做同樣的事,只除了當(dāng)前單元的鍵名也會(huì)在每次循環(huán)中被賦給變量 $key


使用字符數(shù)組時(shí)注意大小寫(xiě),比如range(A,z)和range(a,Z)是不一樣的。

range()函數(shù)還具有第三個(gè)參數(shù),該參數(shù)的作用是設(shè)定步長(zhǎng),比如range(1,9,3)創(chuàng)建的數(shù)組元素是:1、4、7

2,PHP中常規(guī)數(shù)組的排序

一般數(shù)組中的各元素均以字符或數(shù)字表現(xiàn)的,所以可對(duì)數(shù)組元素進(jìn)行升序排列,該功能函數(shù)為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." "; 
?>

升序排序后的數(shù)組元素顯示為 birth name nation sex,當(dāng)然,sort()函數(shù)是區(qū)分字母大小寫(xiě)的(字母從大到小的順序是:A…Z…a…z)

Sort()函數(shù)還具有第二參數(shù),用來(lái)說(shuō)明升序的規(guī)則是用來(lái)比較數(shù)字還是字符串的。比如:

<?php 
    echo "---按數(shù)字升序排序---
    "; 
    $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." "; 
?>

SORT_NUMERIC和SORT_STRING用來(lái)聲明按數(shù)字或字符的升序排列。如果按照數(shù)字升序排列是:3,26;但如果按照字符升序排列則是:26,3了。

PHP中除了升序函數(shù)以外,還有降序或稱(chēng)反向排列的函數(shù),就是rsort()函數(shù),比如:

$num1=range(1,9);
rsort($num1); //這里其實(shí)就相當(dāng)于range(9,1)

3,PHP中關(guān)聯(lián)數(shù)組的排序

PHP除了支持?jǐn)?shù)字索引數(shù)組以外,還支持相關(guān)數(shù)組。比如如下數(shù)組就是一個(gè)相關(guān)(關(guān)聯(lián))數(shù)組:

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

使用sort($peoples)默認(rèn)即是按照元素定義值的升序排序,在關(guān)聯(lián)數(shù)組中可使用asort()函數(shù)表示按元素值升序排序,關(guān)聯(lián)數(shù)組中最主要的則是可按照關(guān)鍵字(如xm、xb、mz等)的升序排序,該方法是用函數(shù)ksort()函數(shù)。

<?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 "
    --按關(guān)鍵字升序排列--
    "; 
    ksort($peoples); 
    foreach ($peoples as $mychrs) 
    echo $mychrs." "; 
?>

和常規(guī)數(shù)組擁有sort()升序函數(shù)的反向排序rsort()降序函數(shù)相對(duì)應(yīng)的,關(guān)聯(lián)數(shù)組也有對(duì)應(yīng)的降序函數(shù):asort()函數(shù)和arsort()函數(shù)、ksort()函數(shù)和krsort()函數(shù)。
記憶:原型函數(shù)是sort(),其中a、k表示關(guān)聯(lián)數(shù)組相關(guān)必須前置,反向排序使用r修飾。

4,PHP數(shù)組元素隨機(jī)排序

PHP中使用shuffle()函數(shù)將數(shù)組元素進(jìn)行隨機(jī)的重新排序,每次都會(huì)顯示不同的排序組合,比如:

<?php 
    $fer=array(&#39;cnbruce&#39;,&#39;cnrose&#39;,&#39;cnjames&#39;,&#39;cnanne&#39;); 
    shuffle($fer); //隨即排序,每刷新一次頁(yè)面則進(jìn)行一次隨機(jī)排序。 
    foreach ($fer as $mychrs) 
    echo $mychrs." "; 
?>

5,PHP數(shù)組按原順序反向排序

PHP中可使用array_reverse()函數(shù)將數(shù)組元素按原順序反向排序。比如:

<?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); //將數(shù)組內(nèi)元素按原順序反向排序 
echo "--按原順序反向--"; 
foreach ($fer as $mychrs) 
echo $mychrs." "; 
?>


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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

How do I stay up-to-date with the latest PHP developments and best practices? How do I stay up-to-date with the latest PHP developments and best practices? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

What is PHP, and why is it used for web development? What is PHP, and why is it used for web development? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

How to set PHP time zone? How to set PHP time zone? Jun 25, 2025 am 01:00 AM

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

How do I validate user input in PHP to ensure it meets certain criteria? How do I validate user input in PHP to ensure it meets certain criteria? Jun 22, 2025 am 01:00 AM

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

What is data serialization in PHP (serialize(), unserialize())? What is data serialization in PHP (serialize(), unserialize())? Jun 22, 2025 am 01:03 AM

ThePhpfunctionSerialize () andunserialize () AreusedtoconvertcomplexdaTastructdestoresintostoraSandaBackagain.1.Serialize () c OnvertsdatalikecarraysorobjectsraystringcontainingTypeandstructureinformation.2.unserialize () Reconstruct theoriginalatataprom

How do I embed PHP code in an HTML file? How do I embed PHP code in an HTML file? Jun 22, 2025 am 01:00 AM

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

What are the best practices for writing clean and maintainable PHP code? What are the best practices for writing clean and maintainable PHP code? Jun 24, 2025 am 12:53 AM

The key to writing clean and easy-to-maintain PHP code lies in clear naming, following standards, reasonable structure, making good use of comments and testability. 1. Use clear variables, functions and class names, such as $userData and calculateTotalPrice(); 2. Follow the PSR-12 standard unified code style; 3. Split the code structure according to responsibilities, and organize it using MVC or Laravel-style catalogs; 4. Avoid noodles-style code and split the logic into small functions with a single responsibility; 5. Add comments at key points and write interface documents to clarify parameters, return values ??and exceptions; 6. Improve testability, adopt dependency injection, reduce global state and static methods. These practices improve code quality, collaboration efficiency and post-maintenance ease.

How do I execute SQL queries using PHP? How do I execute SQL queries using PHP? Jun 24, 2025 am 12:54 AM

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas

See all articles