首頁 後端開發 php教程 字符串函数库-搜索类型_PHP

字符串函数库-搜索类型_PHP

Jun 01, 2016 pm 12:38 PM
string the 函數 字串 搜尋 類型

兼容:(PHP 3, PHP 4, PHP 5)
strpos -- Find position of first occurrence of a string
查找字符在字符串第一次出现的位置

语法:int strpos ( string haystack, mixed needle [, int offset] )
返回值:整数
函数种类: 资料处理

内容说明:
英文:
Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos(), this function can take a full string as the needle parameter and the entire string will be used.
If needle is not found, strpos() will return boolean FALSE.

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack.

中文:
传回参数 needle在字串 haystack中第一次出现的位置,以数字表示。不像strrpos( ),此函式可以取参数 needle全部的字串,而且会使用全部的字串。如果找不到参数 needle,则传回 false。

如果参数 needle不是字串时,它会转换成整数并且按照字元的顺序值来使用。
参数 offset允许你去指定在 haystack中从那一个字元开始搜寻,传回的位置依然是相对於 haystack的起点。

*值得注意的是 needle 只能是一个字符,中文字等就不适合了。

例子讲解:
<font color="#000000"> <font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br>$mystring </font><font color="#007700">= </font><font color="#DD0000">'abc'</font><font color="#007700">;<br><br></font><font color="#0000BB">$findme</font><font color="#007700">= </font><font color="#DD0000">'a'</font><font color="#007700">;<br><br></font><font color="#0000BB">$pos </font><font color="#007700">= </font><font color="#0000BB">strpos</font><font color="#007700">(</font><font color="#0000BB">$mystring</font><font color="#007700">, </font><font color="#0000BB">$findme</font><font color="#007700">);<br><br><br><br></font><font color="#FF8000">// Note our use of ===.Simply == would not work as expected<br><br>// because the position of 'a' was the 0th (first) character.<br><br></font><font color="#007700">if (</font><font color="#0000BB">$pos </font><font color="#007700">=== </font><font color="#0000BB">false</font><font color="#007700">) {<br><br>echo </font><font color="#DD0000">"The string '$findme' was not found in the string '$mystring'\";<br><br>} else {<br><br>echo \"The string '$findme' was found in the string '$mystring'\";<br><br>echo \" and exists at position $pos\";<br><br>}<br><br><br><br>// We can search for the character, ignoring anything before the offset<br><br>$newstring = 'abcdef abcdef';<br><br>$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0<br><br>?></font> </font>
strpos()与substr_count()对比:
<font color="#000000"> <font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br>$mystring </font><font color="#007700">= </font><font color="#DD0000">"Hello Chris\";<br><br>if (substr_count($mystring, \"Hello\") == 0)<br><br>echo \"no\";<br><br>// same as:<br><br>if (strpos($mystring, \"Hello\") === false)<br><br>echo \"no\";<br><br>?></font> </font>

对比下面两个代码注意数字情况下,容易出现的错误,数字整数情况下不能看成字符串。
<font color="#000000"> <font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br>$val1</font><font color="#007700">=</font><font color="#0000BB">123</font><font color="#007700">;<br><br></font><font color="#0000BB">$val2</font><font color="#007700">=</font><font color="#DD0000">"123,456,789\";<br><br>if (strpos($val2, $val1)!==false) echo \"matched\";<br><br>else echo \"not matched\";<br><br>?></font> </font>
<font color="#000000"> <font color="#0000BB">结果为: not matched</font> </font>

<font color="#000000"> <font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br>$val1</font><font color="#007700">=(string)</font><font color="#0000BB">123</font><font color="#007700">;<br><br></font><font color="#0000BB">$val2</font><font color="#007700">=</font><font color="#DD0000">"123,456,789\";<br><br>if (strpos($val2, $val1)!==false) echo \"matched\";<br><br>else echo \"not matched\";<br><br>?></font> </font>
<font color="#000000"> <font color="#0000BB">结果为:not matched</font> </font>

 

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

golang函數動態建立新函數的技巧 golang函數動態建立新函數的技巧 Apr 25, 2024 pm 02:39 PM

Go語言提供了兩種動態函數創建技術:closures和反射。 closures允許存取閉包作用域內的變量,而反射可使用FuncOf函數建立新函數。這些技術在自訂HTTP路由器、實現高度可自訂的系統和建置可插拔的元件方面非常有用。

C++ 函數命名中參數順序的考慮 C++ 函數命名中參數順序的考慮 Apr 24, 2024 pm 04:21 PM

在C++函數命名中,考慮參數順序至關重要,可提高可讀性、減少錯誤並促進重構。常見的參數順序約定包括:動作-物件、物件-動作、語意意義和遵循標準函式庫。最佳順序取決於函數目的、參數類型、潛在混淆和語言慣例。

如何在Java中寫出高效和可維護的函數? 如何在Java中寫出高效和可維護的函數? Apr 24, 2024 am 11:33 AM

編寫高效且可維護的Java函數的關鍵在於:保持簡潔。使用有意義的命名。處理特殊情況。使用適當的可見性。

C++ 函式預設參數與可變參數的優缺點比較 C++ 函式預設參數與可變參數的優缺點比較 Apr 21, 2024 am 10:21 AM

C++函數中預設參數的優點包括簡化呼叫、增強可讀性、避免錯誤。缺點是限制靈活性、命名限制。可變參數的優點包括無限彈性、動態綁定。缺點包括複雜性更高、隱式型別轉換、除錯困難。

excel函數公式大全 excel函數公式大全 May 07, 2024 pm 12:04 PM

1. SUM函數,用於對一列或一組單元格中的數字進行求和,例如:=SUM(A1:J10)。 2、AVERAGE函數,用於計算一列或一組儲存格中的數字的平均值,例如:=AVERAGE(A1:A10)。 3.COUNT函數,用於計算一列或一組單元格中的數字或文字的數量,例如:=COUNT(A1:A10)4、IF函數,用於根據指定的條件進行邏輯判斷,並返回相應的結果。

自訂 PHP 函數和預定義函數之間有什麼區別? 自訂 PHP 函數和預定義函數之間有什麼區別? Apr 22, 2024 pm 02:21 PM

自訂PHP函數與預定義函數的差異在於:作用域:自訂函數僅限於其定義範圍,而預定義函數可在整個腳本中存取。定義方式:自訂函數使用function關鍵字定義,而預先定義函數則由PHP核心定義。參數傳遞:自訂函數接收參數,而預先定義函數可能不需要參數。擴充性:自訂函數可以根據需要創建,而預定義函數是內建的且無法修改。

C++ 函式異常進階:客製化錯誤處理 C++ 函式異常進階:客製化錯誤處理 May 01, 2024 pm 06:39 PM

C++中的異常處理可透過自訂異常類別增強,提供特定錯誤訊息、上下文資訊以及根據錯誤類型執行自訂操作。定義繼承自std::exception的異常類,提供特定的錯誤訊息。使用throw關鍵字拋出自訂異常。在try-catch區塊中使用dynamic_cast將捕獲到的異常轉換為自訂異常類型。在實戰案例中,open_file函數會拋出FileNotFoundException異常,捕捉並處理該異常可提供更具體的錯誤訊息。

C++ 函式中引用參數和指標參數的高階用法 C++ 函式中引用參數和指標參數的高階用法 Apr 21, 2024 am 09:39 AM

C++函數中的引用參數(本質為變數別名,修改引用修改原始變數)和指標參數(儲存原始變數的記憶體位址,透過解引用指標修改變數)在傳遞和修改變數時有著不同的用法。引用參數常用於修改原始變數(尤其是大型結構體),傳遞給建構子或賦值運算子時避免複製開銷。指標參數則用於靈活指向記憶體位置,實現動態資料結構或傳遞空指標表示可選參數。

See all articles