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

Home php教程 php手冊(cè) PHP編程中字符串處理的5個(gè)技巧小結(jié)

PHP編程中字符串處理的5個(gè)技巧小結(jié)

Jun 13, 2016 pm 12:30 PM
php the difference apostrophe and deal with character string quotation marks Skill Notice of programming escape

字符串
注意單引號(hào)和雙引號(hào)的區(qū)別
注意轉(zhuǎn)義字符\的使用\\,\",\$
注意使用8進(jìn)制或16進(jìn)制字符表示?\xf6
echo?"H\xf6me";//需要察看是否支持此類(lèi)文字編碼
---------------------輸出結(jié)果----------------------------------------
H鰉e
---------------------------------------------------------------------

1.使用printf()和sprintf()創(chuàng)建格式化的輸出

printf()直接輸出到輸出緩沖區(qū)
sprintf()的輸出作為字符串返回
如printf("輸出內(nèi)容?%.2f\n",$PI());
所有的轉(zhuǎn)換規(guī)范都以%開(kāi)頭
數(shù)據(jù)類(lèi)型有d-整數(shù),s-字符串,f-浮點(diǎn)數(shù),b-二進(jìn)制
.2是一個(gè)可選的寬度指標(biāo),小數(shù)點(diǎn)右邊輸出使用0填充
printf("%.2f",3.14159);
printf("%10.2f",3.14159);
printf("%.10f",3.14159);
printf("%.9s",abcdefghijklmn);
printf("%5.2f,%f,%7.3f\m",3.14159,3.14159,3.14159);
printf("%b?%d?%f?%s?\n",123,123,123,"test");
---------------------輸出結(jié)果----------------------------------------
3.14?3.143.1415900000abcdefghi?3.14,3.141590,?3.142\m1111011?123?123.000000?test?
---------------------------------------------------------------------

2.字符串填充

string?str_pad(string?input原始字串,?int?length添加后的總長(zhǎng)度[,?string?padding要填充的字符?[,?int?pad_type]填充類(lèi)型])
填充類(lèi)型有添加在左邊STR_PAD_LEFT,默認(rèn)添在右邊,填充在兩端STR_PAD_BOTH
$index?=?array("one"=>1,"two"=>155,"three"=>1679);
echo?"
";
echo?str_pad("這是標(biāo)題",50,"?",STR_PAD_BOTH)."\n";
foreach($index?as?$inkey=>$inval)
????????echo?str_pad($inkey,30,".").str_pad($inval,20,".",STR_PAD_LEFT)."\n";
echo?"
";
---------------------輸出結(jié)果----------------------------------------

?????????????????????這是標(biāo)題?????????????????????
one..............................................1
two............................................155
three.........................................1679

---------------------------------------------------------------------
string?strtolower(string?subject)//轉(zhuǎn)換為小寫(xiě)
string?strtoupper(string?subject)//轉(zhuǎn)換為大寫(xiě)
string?ucfirst(string?subject)//首字母大寫(xiě)
string?ucwords(string?subject)//每個(gè)單詞首字母大寫(xiě)
string?ltrim(string?subject)//去左空白
string?rtrim(string?subject)//去右空白
string?trim(string?subject)去左右空白,空白包括null,制表符,換行符,回車(chē)符和空格
string?n12br(string?source)//將\n表示的換行符轉(zhuǎn)換為
標(biāo)記

3.字符串比較

integer?strcmp(sting?str1,string?str2)?//str1大于str2返回-1?str1小于str2返回1?str1和str2相等返回0?
integer?strmcmp(sting?str1,string?str2,integer?length)?//第三個(gè)參數(shù)限制length個(gè)字符的比較
print?strcmp("aardvark","aardwolf");
print?strncmp("aardvark","aardwolf",4);
---------------------輸出結(jié)果----------------------------------------
-10
---------------------------------------------------------------------
strcasecmp()和strncasecmp()是不區(qū)分大小寫(xiě)的比較函數(shù)

4.查找和抽取子字符串

string?substr(sting?source,integer?start[,integer?length])//從start開(kāi)始取length個(gè)字符
start和length可以使用負(fù)值
$var?=?"abcdefgh";
print?substr($var,2);//從0開(kāi)始計(jì)數(shù)
print?substr($var,2,3);
print?substr($var,-1);//從字符串的末尾開(kāi)始
print?substr($var,-5,2);
print?substr($var,-5,-2);
---------------------輸出結(jié)果----------------------------------------
cdefgh
cde
h
de
def
---------------------------------------------------------------------
integer?strpos(string?haystack,string?needle[,integer?offset])//查找子字符串的位置,返回第一次出現(xiàn).
integer?strrpos(string?haystack,string?needle)//只搜索單個(gè)字符(多個(gè)字符只取第一個(gè)),返回最后一次出現(xiàn)的索引.
還有常見(jiàn)的從?字符串中抽取找到的部分?的函數(shù)
string?strstr(string?haystack,string?needle)//不區(qū)分大小寫(xiě)
string?stristr(string?haystack,string?needle)//區(qū)分大小寫(xiě)
string?strrchr(string?haystack,sting?needle)
***********?array?explode(string?separator,string?subject[,integer?limit])//返回一個(gè)字符串?dāng)?shù)組
array?implode(string?glue,array?pieces)//返回一個(gè)字符串
///////////////////////////代碼段////////////////////////////////////////
$guest?=?"this?is?a?string";
$guestArray?=?explode("?",$guest);
var_dump($guestArray);
sort($guestArray);
echo?implode(",",$guestArray);
////////////////////////////////////////////////////////////////////////
---------------------輸出結(jié)果----------------------------------------
array(4)?{?[0]=>?string(4)?"this"?[1]=>?string(2)?"is"?[2]=>?string(1)?"a"?[3]=>?string(6)?"string"?}?a,is,string,this
---------------------------------------------------------------------

5.替換字符和子字符串

string?substr_replace(string?source,string?replace,int?start[,int?length])?

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)

Hot Topics

PHP Tutorial
1502
276
VSCode settings.json location VSCode settings.json location Aug 01, 2025 am 06:12 AM

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

Python for Data Engineering ETL Python for Data Engineering ETL Aug 02, 2025 am 08:48 AM

Python is an efficient tool to implement ETL processes. 1. Data extraction: Data can be extracted from databases, APIs, files and other sources through pandas, sqlalchemy, requests and other libraries; 2. Data conversion: Use pandas for cleaning, type conversion, association, aggregation and other operations to ensure data quality and optimize performance; 3. Data loading: Use pandas' to_sql method or cloud platform SDK to write data to the target system, pay attention to writing methods and batch processing; 4. Tool recommendations: Airflow, Dagster, Prefect are used for process scheduling and management, combining log alarms and virtual environments to improve stability and maintainability.

How to obtain digital currency BTC? What are the differences between btc and digital currency? How to obtain digital currency BTC? What are the differences between btc and digital currency? Aug 01, 2025 pm 11:15 PM

There are four main ways to obtain BTC: 1. Register and exchange it with fiat currency or other digital assets through centralized trading platforms such as Binance, OK, Huobi, and Gate.io; 2. Participate in P2P platforms to directly trade with individuals, and pay attention to the credit risks of the counterparty; 3. Provide goods or services to accept BTC as payment; 4. Participate in airdrops, competitions and other platform reward activities to obtain a small amount of BTC. The core difference between BTC and digital currency is: 1. BTC is a type of digital currency, which belongs to a genus relationship; 2. BTC adopts a proof of work (PoW) mechanism, while other digital currencies may use various technologies such as proof of stake (PoS); 3. BTC emphasizes the value storage function of "digital gold", and other digital currencies may focus on payment efficiency or

Using PHP for Data Scraping and Web Automation Using PHP for Data Scraping and Web Automation Aug 01, 2025 am 07:45 AM

UseGuzzleforrobustHTTPrequestswithheadersandtimeouts.2.ParseHTMLefficientlywithSymfonyDomCrawlerusingCSSselectors.3.HandleJavaScript-heavysitesbyintegratingPuppeteerviaPHPexec()torenderpages.4.Respectrobots.txt,adddelays,rotateuseragents,anduseproxie

Google Chrome cannot open local files Google Chrome cannot open local files Aug 01, 2025 am 05:24 AM

ChromecanopenlocalfileslikeHTMLandPDFsbyusing"Openfile"ordraggingthemintothebrowser;ensuretheaddressstartswithfile:///;2.SecurityrestrictionsblockAJAX,localStorage,andcross-folderaccessonfile://;usealocalserverlikepython-mhttp.server8000tor

Understanding Network Ports and Firewalls Understanding Network Ports and Firewalls Aug 01, 2025 am 06:40 AM

Networkportsandfirewallsworktogethertoenablecommunicationwhileensuringsecurity.1.Networkportsarevirtualendpointsnumbered0–65535,withwell-knownportslike80(HTTP),443(HTTPS),22(SSH),and25(SMTP)identifyingspecificservices.2.PortsoperateoverTCP(reliable,c

Using HTML `input` Types for User Data Using HTML `input` Types for User Data Aug 03, 2025 am 11:07 AM

Choosing the right HTMLinput type can improve data accuracy, enhance user experience, and improve usability. 1. Select the corresponding input types according to the data type, such as text, email, tel, number and date, which can automatically checksum and adapt to the keyboard; 2. Use HTML5 to add new types such as url, color, range and search, which can provide a more intuitive interaction method; 3. Use placeholder and required attributes to improve the efficiency and accuracy of form filling, but it should be noted that placeholder cannot replace label.

go by example http middleware logging example go by example http middleware logging example Aug 03, 2025 am 11:35 AM

HTTP log middleware in Go can record request methods, paths, client IP and time-consuming. 1. Use http.HandlerFunc to wrap the processor, 2. Record the start time and end time before and after calling next.ServeHTTP, 3. Get the real client IP through r.RemoteAddr and X-Forwarded-For headers, 4. Use log.Printf to output request logs, 5. Apply the middleware to ServeMux to implement global logging. The complete sample code has been verified to run and is suitable for starting a small and medium-sized project. The extension suggestions include capturing status codes, supporting JSON logs and request ID tracking.

See all articles