explode

PHP: How can I explode a string by commas, but not wheres the commas are within quotes?

隐身守侯 提交于 2019-11-26 08:57:11
问题 I need to explode my string input into an array at the commas. However the string contains commas inside quotes. Input: $line = \'TRUE\',\'59\',\'A large number is 10,000\'; $linearray = explode(\",\",$line); $linemysql = implode(\"\',\'\",$linearray); Returns $linemysql as: \'TRUE\',\'59\',\'A large number is 10\',\'000\' How can I go about accomplishing this, with the explode ignoring the commas inside the quote marks? 回答1: Since you are using comma seperated values, you can use str_getcsv.

PHP Split Delimited String into Key/Value Pairs (Associative Array)

≡放荡痞女 提交于 2019-11-26 08:56:56
I have a string like this: key1\value1\key2\value2\key3\value3\key4\value4\key5\value5 And I'd like it to be an associative array so that I can do: echo $myArray['key1']; // prints value1 echo $myArray['key3']; // prints value3 //etc... I know I can explode on the backslash, but not sure how to go from there. mario Using a simple regex via preg_match_all and array_combine is often the shortest and quickest option: preg_match_all("/([^\\\\]+)\\\\([^\\\\]+)/", $string, $p); $array = array_combine($p[1], $p[2]); Now this is of course a special case. Both keys and values are separated by a \

for和foreach效率比较

那年仲夏 提交于 2019-11-26 08:11:54
for和foreach效率比较 $arr = array('rFG3','rShJ','pARu',.....); // 8000条数据 1. for循环 $starttime = explode(' ',microtime()); for($i=0;$i<count($arr);$i++){ $i; } $endtime = explode(' ',microtime()); $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]); echo "执行耗时:".$thistime." 秒。"; 执行是输出:执行耗时:0.033001899719238 秒。 2. for count // 把count()函数提出来再执行for循环 $starttime = explode(' ',microtime()); $count = count($arr); for($i=0;$i<$count;$i++){ $i; } $endtime = explode(' ',microtime()); $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]); echo "执行耗时:".$thistime." 秒。"; 执行是输出:执行耗时:0

Add a prefix to each item of a PHP array

与世无争的帅哥 提交于 2019-11-26 05:25:27
问题 I have a PHP array of numbers, which I would like to prefix with a minus (-). I think through the use of explode and implode it would be possible but my knowledge of php is not possible to actually do it. Any help would be appreciated. Essentially I would like to go from this: $array = [1, 2, 3, 4, 5]; to this: $array = [-1, -2, -3, -4, -5]; Any ideas? 回答1: Simple: foreach ($array as &$value) { $value *= (-1); } unset($value); Unless the array is a string: foreach ($array as &$value) { $value

how to convert array values from string to int?

半腔热情 提交于 2019-11-26 03:49:17
问题 $string = \"1,2,3\" $ids = explode(\',\', $string); var_dump($ids); returns array(3) { [0]=> string(1) \"1\" [1]=> string(1) \"2\" [2]=> string(1) \"3\" } I need for the values to be of type int instead of type string. Is there a better way of doing this than looping through the array with a foreach and converting each string to int? 回答1: You can achieve this by following code, $integerIDs = array_map('intval', explode(',', $string)); 回答2: This is almost 3 times faster than explode() , array

PHP explode the string, but treat words in quotes as a single word

余生颓废 提交于 2019-11-26 02:09:24
问题 How can I explode the following string: Lorem ipsum \"dolor sit amet\" consectetur \"adipiscing elit\" dolor into array(\"Lorem\", \"ipsum\", \"dolor sit amet\", \"consectetur\", \"adipiscing elit\", \"dolor\") So that the text in quotation is treated as a single word. Here\'s what I have for now: $mytext = \"Lorem ipsum %22dolor sit amet%22 consectetur %22adipiscing elit%22 dolor\" $noquotes = str_replace(\"%22\", \"\", $mytext\"); $newarray = explode(\" \", $noquotes); but my code divides

PHP Split Delimited String into Key/Value Pairs (Associative Array)

寵の児 提交于 2019-11-26 02:02:10
问题 I have a string like this: key1\\value1\\key2\\value2\\key3\\value3\\key4\\value4\\key5\\value5 And I\'d like it to be an associative array so that I can do: echo $myArray[\'key1\']; // prints value1 echo $myArray[\'key3\']; // prints value3 //etc... I know I can explode on the backslash, but not sure how to go from there. 回答1: Using a simple regex via preg_match_all and array_combine is often the shortest and quickest option: preg_match_all("/([^\\\\]+)\\\\([^\\\\]+)/", $string, $p); $array