数据库拆分字符串函数_PHP | 不使用库函数将逗号分隔的字符串拆分为数组
數據庫拆分字符串函數
Given a string with comma delimited, we have to split it into an array.
給定一個以逗號分隔的字符串,我們必須將其拆分為一個數組。
Example:
例:
Input: "Google,Bing,Yahoo!,DuckDuckGo"Output:arrar of strings after splitting...Array([0] => Google [1] => Bing [2] => Yahoo! [3] => DuckDuckGo)PHP代碼將逗號分隔的字符串拆分為數組,而無需使用庫函數 (PHP code to split comma delimited string into an array without using library function)
<?php //PHP code to reverse the string without //using library function//function definition //it accepts a string and returns an array //delimited by commas function split_string($text){//variable to store the result i.e. an array $arr = [];//calculate string length$strLength = strlen($text);$dl = ','; //delimeter$j = 0;$tmp = ''; //a temp variable//logic - it will check all characters//and split the string when comma foundfor ($i = 0; $i < $strLength; $i++) {if($dl === $text[$i]) {$j++;$tmp = '';continue;}$tmp .= $text[$i];$arr[$j] = $tmp;}//return the resultreturn $arr; }//main code i.e. function calling $str = "New Delhi,Mumbai,Chennai,Banglore"; $result = split_string($str); echo "string is: " .$str. "<br/>"; echo "arrar of strings after splitting..."."<br/>"; print_r($result);$str = "Google,Bing,Yahoo!,DuckDuckGo"; $result = split_string($str); echo "string is: " .$str. "<br/>"; echo "arrar of strings after splitting..."."<br/>"; print_r($result); ?>Output
輸出量
string is: New Delhi,Mumbai,Chennai,Banglore arrar of strings after splitting... Array ([0] => New Delhi[1] => Mumbai[2] => Chennai[3] => Banglore ) string is: Google,Bing,Yahoo!,DuckDuckGo arrar of strings after splitting... Array ([0] => Google [1] => Bing [2] => Yahoo! [3] => DuckDuckGo )Explanation:
說明:
We use a for loop to convert our comma delimited string into an array. We identify when a (,) appears in the string and copy that into an array then follow this process until the whole length of the string is covered. The inverted string is stored into a temporary variable ($tmp) then moved to an array ($arr[]).
我們使用for循環將逗號分隔的字符串轉換為數組。 我們確定字符串中何時出現( , ),然后將其復制到數組中,然后執行此過程,直到覆蓋整個字符串。 倒置的字符串存儲到一個臨時變量( $ tmp )中,然后移到一個數組( $ arr [] )中。
翻譯自: https://www.includehelp.com/php/split-comma-delimited-string-into-an-array-without-using-library-function.aspx
數據庫拆分字符串函數
總結
以上是生活随笔為你收集整理的数据库拆分字符串函数_PHP | 不使用库函数将逗号分隔的字符串拆分为数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java CharArrayReader
- 下一篇: kotlin 查找id_Kotlin程序