日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

数据库拆分字符串函数_PHP | 不使用库函数将逗号分隔的字符串拆分为数组

發(fā)布時間:2025/3/11 php 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据库拆分字符串函数_PHP | 不使用库函数将逗号分隔的字符串拆分为数组 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

數(shù)據(jù)庫拆分字符串函數(shù)

Given a string with comma delimited, we have to split it into an array.

給定一個以逗號分隔的字符串,我們必須將其拆分為一個數(shù)組。

Example:

例:

Input: "Google,Bing,Yahoo!,DuckDuckGo"Output:arrar of strings after splitting...Array([0] => Google [1] => Bing [2] => Yahoo! [3] => DuckDuckGo)

PHP代碼將逗號分隔的字符串拆分為數(shù)組,而無需使用庫函數(shù) (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循環(huán)將逗號分隔的字符串轉(zhuǎn)換為數(shù)組。 我們確定字符串中何時出現(xiàn)( , ),然后將其復(fù)制到數(shù)組中,然后執(zhí)行此過程,直到覆蓋整個字符串。 倒置的字符串存儲到一個臨時變量( $ tmp )中,然后移到一個數(shù)組( $ arr [] )中。

翻譯自: https://www.includehelp.com/php/split-comma-delimited-string-into-an-array-without-using-library-function.aspx

數(shù)據(jù)庫拆分字符串函數(shù)

總結(jié)

以上是生活随笔為你收集整理的数据库拆分字符串函数_PHP | 不使用库函数将逗号分隔的字符串拆分为数组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。