首頁  >  文章  >  後端開發  >  PHP 字串

PHP 字串

PHPz
PHPz原創
2024-08-29 12:55:08456瀏覽

以下文章提供了 PHP strpos 的概述。有大量內建函數可以讓編碼器或開發人員更輕鬆地進行字串相關操作。 strops() 是流行的字串函數之一,顧名思義,我們可以根據需要使用它來處理 PHP 中任何字串中的位置。使用這個函數,我們可以取得該字串在另一個字串中的位置。這個函數可以以多種方式用於各種場景,例如我們可以取得字串在另一個字串中的首次出現位置。我們可以得到該字串在另一個字串中最後出現的位置。此函數也可用於將一個字串搜尋到另一個字串。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

PHP strpos 語法

由於它可以以多種方式使用,因此我們可以看到各種語法:

strpos($string, $toBeSearched)

參數說明:

  • $string:這是需要進行搜尋的字串。
  • $toBeSearched:這是一個需要在上面字串中搜尋的字串。

回傳類型:

如果搜尋找到則傳回數值;否則它給出錯誤。

stripos($string, $toBeSearched, $startPostion)

除了前面的參數之外,我們還可以看到一個額外的參數。 $startPostion 沒什麼,但它是關於從哪裡開始搜尋的。

strrpos($string, $toBeSearched, $startPostion)

此函數可用來將字串搜尋到另一個字串。這個開始是以相反的順序搜尋的。該函數中間的單字「r」起到了以相反順序將字串搜尋到另一個字串的技巧。

strpos() 函數在 PHP 中如何運作?

  • 要使用 strpos() 函數和其他相關函數,我們需要有兩個字串,一個字串將被搜尋到另一個字串。
  • 我們還有各種其他內建函數,如 stripos()、strrpos()、strripos() 等。 stripos() 可用來搜尋不區分大小寫的搜尋字串。 Strrpos() 可用於逆序搜尋字串。
  • strripos() 可用於以不區分大小寫的搜尋字串的相反順序搜尋字串。

PHP strpos() 範例

下面給出的是提到的例子:

範例#1

使用 strops() 函數的基本程序,只有兩個參數,字串一和搜尋字串。

代碼:

<?php
$string = "I love php, I love coding in php too!";
$searchString = "php";
$output =  strpos($string, $searchString);
echo "String:. “ $string;
echo "<br>Output: ". $output;
?>

輸出:

PHP 字串

在上面的範例中,我們可以看到,在一個字串中,我們嘗試搜尋PHP 字串,使用strops() 搜尋PHP 單字給出了輸出7.7 是第一次出現的索引PHP 單詞。 strops() 是一個區分大小寫的函數,用於尋找另一個字串中的任何字串。

範例#2

讓我們嘗試複製與範例 1 相同的內容,但使用 PHP 的不同大小寫(大寫或小寫)。

代碼:

<?php
$string = "I love php, I love coding in php too!";
$searchString = "PHP";
$output =  strpos($string, $searchString); // this will give the false result
echo "String: ". $string;
echo "<br>Output: ". $output;
$output1 =  stripos($string, $searchString); // this will give the the 7 as a result
echo "<br>String: ". $string;
echo "<br>Output: ". $output1;
?>

輸出:

PHP 字串

在上面的範例中,我們可以看到,如果情況不匹配,則strops() 將不會完成這項工作,為此,我們有stripos() 具有相同的語法和參數來執行相同的工作。

範例#3

讓我們來看看將第三個參數與 strops() 一起使用的範例。

代碼:

<?php
$string = "I love php, I love coding in php too!";
$searchString = "PHP";
$output =  strpos($string, $searchString,8); // this will give the false result
echo "String: ". $string;
echo "<br>Output: ". $output;
$output1 =  stripos($string, $searchString,8); // this will give the the 7 as a result
echo "<br>String: ". $string;
echo "<br>Output: ". $output1;
?>

輸出:

PHP 字串

與前面的範例稍有不同就會產生巨大的差異,正如我們在結果中看到的那樣。我們將 8 作為第三個參數。使用 8 作為第三個參數意味著搜尋將從位置 8 開始,而不是從位置 0 開始。

範例#4

讓我們來看一些專業的例子(處理退貨類型)。

代碼:

<?php
$string = "https://www.educba.com/tutorials/?source=menu";
$searchString = "tutorials";
$output =  strpos($string, $searchString); // this will give the false result
if($output==false){
echo "Search not found.";
}else{
echo "Search found.";
}
?>

輸出:

PHP 字串

範例#5

strrpos() 可用於以相反的順序將字串的位置搜尋到另一個字串中。

代碼:

<?php
$string = "I love PHP; I like to code in PHP";
$searchString = "PHP";
$output =  strrpos($string, $searchString); // this will give the false result
if($output==false){
echo "Search not found.";
}else{
echo "Search found: ".$output;
}
?>

輸出:

PHP 字串

同樣的方式,我們可以使用 strripos() 函數將字串搜尋到另一個不區分大小寫的搜尋字串中。現在,我們已經看到了可以根據業務需求使用的所有 strops() 類型的範例。

結論

在 PHP 程式語言中,我們可以使用 strpose() 和 stripos() 內建函數將一個字串搜尋到另一個字串。 strops() 是一個區分大小寫的函數來處理搜索,在 PHP 中,stripos() 可以用於不區分大小寫,以完成與 strops() 相同的工作。 stripos() 不檢查相同的情況。開發人員在使用這些函數時必須非常小心,因為如果搜尋匹配,它會給出數字作為結果,但如果沒有找到搜索,它會返回 false。

以上是PHP 字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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