This question already has answers here:
PHP CSV string to array
(10 answers)
Closed 6 years ago.
I have a string like this:
'test', 'test', 'test, test', NULL, NULL, NULL, 123456789012, 0, '2017-02-17', FALSE
I want to explode it into an array.
But this gets messed up when a partial string contains a comma ('test, test').
How can I replace commas within a partial string into some other character? (so explode will work).
Apostrophes in the string must be contained, so str_getcsv() can not be used.
Here's my way:
$string = "'test', 'test', 'test, test, kk', NULL, NULL, NULL, 123456789012, 0, '2017-02-17', FALSE";
$array_tmp = explode(', ', $string);
$array = array();
$index_buffer = NULL;
$index = 0;
foreach($array_tmp as $value) {
// Check if we need to append to buffered entry
if($index_buffer !== NULL){
$array[$index_buffer] .= ', ' . $value;
if($value[strlen($value) - 1] === "'"){
$index_buffer = NULL;
}
continue;
}
// Check if it's not ended string
if(is_string($value) && $value[0] === "'" && $value[strlen($value) - 1] !== "'"){
// It is not ended, set this index as buffer
$index_buffer = $index;
}
// Save value
$array[$index] = $value;
$index++;
}
echo '<pre>' . print_r($array, true);
Output:
Array
(
[0] => 'test'
[1] => 'test'
[2] => 'test, test, kk'
[3] => NULL
[4] => NULL
[5] => NULL
[6] => 123456789012
[7] => 0
[8] => '2017-02-17'
[9] => FALSE
)
Or this may be more suitable but you lose quotes, and I guess, if your input string doesn't respect all csv standards, you could have border effects since str_getcsv handles more things than this quote issue:
str_getcsv($string, ",", "'");
You can do this manually and improve it to support more cases... Try something like this:
$arr = array();
$arr[0] = "";
$arrIndex = 0;
$strOpen = false;
for ($i = 0; $i < mb_strlen($str); $i++){
if ($str[$i] == ',') {
if ($strOpen == false) {
$arrIndex++;
$arr[$arrIndex] = "";
}
else {
$arr[$arrIndex] .= $str[$i];
}
}
else if ($str[$i] == '\'') {
$strOpen = !$strOpen;
}
else {
$arr[$arrIndex] .= $str[$i];
}
}
Result:
Array
(
[0] => test
[1] => test
[2] => test, test
[3] => NULL
[4] => NULL
[5] => NULL
[6] => 123456789012
[7] => 0
[8] => 2017-02-17
[9] => FALSE
)
Note: it will keep "empty" spaces around comas
Try using str_getcsv
str_getcsv($string, ",", "'");
Related
I am using PHP 7.3.5 and I have the following set of array values:
$valueArr = ['-4.2%', '51.0', '90K', '0.5%', '0.74|2.6', '-1.2B', '779B', '215K', '92.2%', '42.8B', '1.49T', '1690B', '-10.8B', '0.38|3.9', '102.4', '1.00%', '0.07|1.3'];
Basically I want for each of these values the number and the "type", so if it is a percentage then I would like to get -4.2 and percentage.
I tried to create a minimum example (however the below code is no real good example ;( ), but I am stuck at the data structure level as some array keys have two inputs, such as '0.74|2.6':
<?php
$valueArr = ['-4.2%', '51.0', '90K', '0.5%', '0.74|2.6', '-1.2B', '779B', '215K', '92.2%', '42.8B', '1.49T', '1690B', '-10.8B', '0.38|3.9', '102.4', '1.00%', '0.07|1.3'];
$resArr = array();
$structureArr = array(
'value1' => "",
'number1' => "",
'value2' => "",
'number2' => ""
);
foreach ($valueArr as $key => $v) {
if (1 === preg_match('/%/', $valueArr[$key])) {
preg_match('!\d+\.*\d*!', $valueArr[$key], $structureArr['number1']);
$structureArr['value1'] = 'percentage';
}
/*
if (1 === preg_match('|', $valueArr[$key])) {
$str = explode("|", $valueArr[$key]);
$value1 = 'number';
$number1 = $str[0];
$value2 = 'number';
$number2 = $str[1];
}
if (1 === preg_match('', $valueArr[$key])) {
}
*/
array_push($resArr, $structureArr);
}
print_r($resArr);
/*
Wanted Result
Array
(
[0] => Array
(
[0] => -4.2
[1] => 'percentage'
)
[1] => Array
(
[0] => 51.0
[1] => 'number'
)
[2] => Array
(
[0] => 90000
[1] => number
)
[3] => Array
(
[0] => 0.5
[1] => percentage
)
[4] => Array
(
[0] => 0.74
[1] => number
[2] => 2.6
[3] => number
)
...
*/
I would highly appreciate your input on how to structure this array input.
Appreciate your replies!
If you join the array on a space and replace pipes | with a space, then you have a list of numbers and their symbol (if any) separated by a space. Then just match your numbers and whatever symbol comes after it. Then you just match the number index with the symbol index. I used an array to map the symbol to the word and number if none:
$string = str_replace('|', ' ', implode(' ', $valueArr));
preg_match_all('/([\d.-]+)([^\s]*)/', $string, $matches);
$types = ['%'=>'percent','K'=>'thousand','M'=>'million','B'=>'billion','T'=>'trillion'];
foreach($matches[1] as $k => $v) {
$t = $types[$matches[2][$k]] ?? 'number';
$result[] = [$v, $t];
}
This yields an array like this, with each number that was joined by a pipe with it's own element:
Array
(
[0] => Array
(
[0] => -4.2
[1] => percent
)
[1] => Array
(
[0] => 51.0
[1] => number
)
[2] => Array
(
[0] => 90
[1] => thousand
)
///etc...
If you need a floating point number then just change:
$result[] = [(float)$v, $t];
This expands on my comment. Not sure if it's the most optimal solution or not.
Rough outline...
Create array mapping suffix to multiplier. Loop through source array. explode on |. Loop through result. If last character is %, strip it, value=value and type=percentage, else, strip last char, use it as array index (if it is an available index), value=value*multiplier and type=number.
$resArr = array();
$multipliers = array("K" => 1000, "M" => 1000000, "B" => 1000000000, "T" => 1000000000000);
$valueArr = ['-4.2%', '51.0', '90K', '0.5%', '0.74|2.6', '-1.2B', '779B', '215K', '92.2%', '42.8B', '1.49T', '1690B', '-10.8B', '0.38|3.9', '102.4', '1.00%', '0.07|1.3'];
foreach($valueArr as $index => $value)
{
$parts = explode("|", $value);
$resArr[$index] = array();
foreach($parts as $part)
{
$lastChar = substr($part, -1);
if($lastChar == "%")
{
$resArr[$index][] = substr($part, 0, -1);
$resArr[$index][] = "percentage";
}
else if(in_array($lastChar, array_keys($multipliers)))
{
$multiple = $multipliers[$lastChar];
$resArr[$index][] = (substr($part, 0, -1))*$multiple;
$resArr[$index][] = "number";
}
else
{
$resArr[$index][] = $part;
$resArr[$index][] = "number";
}
}
}
var_dump($resArr);
DEMO
i hope someone can help.
I have a string as following
$string = 'latitude=46.6781471,longitude=13.9709534,options=[units=auto,lang=de,exclude=[hourly,minutely]]';
Now what i am trying is to create an array out of each key, value pair but badly failing with regex for preg_match_all()
Currently my attempts aren't giving desired results, creating key => value pairs works as long as there are no brackets, but i have absolutely no idea how to achieve a multidimensional array if key contains key/value pairs inside brackets in example.
Array (
[0] => Array
(
[0] => latitude=46.6781471,
[1] => longitude=13.9709534,
[2] => options=[units=si,
[3] => lang=de,
)
[1] => Array
(
[0] => latitude
[1] => longitude
[2] => options=[units
[3] => lang
)
.. and so on
Where in the end i would like to achieve results as following.
Array (
[latitude] => 46.6781471
[longitude] => 13.9709534
[options] => Array
(
[units] => auto
[exclude] => hourly,minutely
)
)
I would appreciate any help or example how i can achieve this from a given string.
Regular expression isn't the right tool to deal with recursive matches. You can write a parser instead of a regex (or use JSON, query string, XML or any other commonly used format):
function parseOptionsString($string) {
$length = strlen($string);
$key = null;
$contextStack = array();
$options = array();
$specialTokens = array('[', ']', '=', ',');
$buffer = '';
$currentOptions = $options;
for ($i = 0; $i < $length; $i++) {
$currentChar = $string[$i];
if (!in_array($currentChar, $specialTokens)) {
$buffer .= $currentChar;
continue;
}
if ($currentChar == '[') {
array_push($contextStack, [$key, $currentOptions]);
$currentOptions[$key] = array();
$currentOptions = $currentOptions[$key];
$key = '';
$buffer = '';
continue;
}
if ($currentChar == ']') {
if (!empty($buffer)) {
if (!empty($key)) {
$currentOptions[$key] = $buffer;
} else {
$currentOptions[] = $buffer;
}
}
$contextInfo = array_pop($contextStack);
$previousContext = $contextInfo[1];
$thisKey = $contextInfo[0];
$previousContext[$thisKey] = $currentOptions;
$currentOptions = $previousContext;
$buffer = '';
$key = '';
continue;
}
if ($currentChar == '=') {
$key = $buffer;
$buffer = '';
continue;
}
if ($currentChar == ',') {
if (!empty($key)) {
$currentOptions[$key] = $buffer;
} else if (!empty($buffer)) {
$currentOptions[] = $buffer;
}
$buffer = '';
$key = '';
continue;
}
}
if (!empty($key)) {
$currentOptions[$key] = $buffer;
}
return $currentOptions;
}
this gives the following output:
print_r(parseOptionsString($string));
Array
(
[latitude] => 46.6781471
[longitude] => 13.9709534
[options] => Array
(
[units] => auto
[lang] => de
[exclude] => Array
(
[0] => hourly
[1] => minutely
)
)
)
Note also that you want a special syntax for arrays with only comma separated values (exclude=[hourly,minutely] becomes exclude => hourly,minutely and not exclude => array(hourly, minutely)). I think this is an inconsistency in your format and I wrote the parser with the "correct" version in mind.
If you don't want parser you can also try this code. It converts your string to JSON and decode to array. But as others said, I think you should try the approach with JSON. If you're sending this string by XmlHttpRequest in JavaScript it will not be hard to create proper JSON code to send.
$string = 'latitude=46.6781471,longitude=13.9709534,options=[units=auto,lang=de,exclude=[hourly,minutely]]';
$string = preg_replace('/([^=,\[\]\s]+)/', '"$1"', $string);
$string = '{' . $string . '}';
$string = str_replace('=', ':', $string);
$string = str_replace('[', '{', $string);
$string = str_replace(']', '}', $string);
$string = preg_replace('/({[^:}]*})/', '|$1|', $string);
$string = str_replace('|{', '[', $string);
$string = str_replace('}|', ']', $string);
$result = json_decode($string, true);
print_r($result);
I have this file format of txt file generated from schematic software:
(
NETR5_2
R6,1
R5,2
)
(
NETR1_2
R4,2
R3,1
R3,2
R2,1
R2,2
R1,1
R1,2
)
I need to get this:
Array
(
[0] => Array
(
[0] => NETR5_2
[1] => R6,1
[2] => R5,2
)
[1] => Array
[0] => NETR1_2
[1] => R4,2
[2] => R3,1
[3] => R3,2
[4] => R2,1
[5] => R2,2
[6] => R1,1
[7] => R1,2
)
Here is code i try but i get all from input string:
$file = file('tangoLista.txt');
/* GET - num of lines */
$f = fopen('tangoLista.txt', 'rb');
$lines = 0;
while (!feof($f)) {
$lines += substr_count(fread($f, 8192), "\n");
}
fclose($f);
for ($i=0;$i<=$lines;$i++) {
/* RESISTORS - check */
if (strpos($file[$i-1], '(') !== false && strpos($file[$i], 'NETR') !== false) {
/* GET - id */
for($k=0;$k<=10;$k++) {
if (strpos($file[$i+$k], ')') !== false || empty($file[$i+$k])) {
} else {
$json .= $k.' => '.$file[$i+$k];
}
}
$resistors_netlist[] = array($json);
}
}
echo '<pre>';
print_r($resistors_netlist);
echo '</pre>';
I need to read between ( and ) and put into array values...i try using checking if line begins with ( and NETR and if yes put into array...but i don't know how to get number if items between ( and ) to get foreach loop to read values and put into array.
Where i im making mistake? Can code be shorter?
Try this approach:
<?php
$f = fopen('test.txt', 'rb');
$resistors_netlist = array();
$current_index = 0;
while (!feof($f)) {
$line = trim(fgets($f));
if (empty($line)) {
continue;
}
if (strpos($line, '(') !== false) {
$resistors_netlist[$current_index] = array();
continue;
}
if (strpos($line, ')') !== false) {
$current_index++;
continue;
}
array_push($resistors_netlist[$current_index], $line);
}
fclose($f);
print_r($resistors_netlist);
This gives me:
Array
(
[0] => Array
(
[0] => NETR5_2
[1] => R6,1
[2] => R5,2
)
[1] => Array
(
[0] => NETR1_2
[1] => R4,2
[2] => R3,1
[3] => R3,2
[4] => R2,1
[5] => R2,2
[6] => R1,1
[7] => R1,2
)
)
We start $current_index at 0. When we see a (, we create a new sub-array at $resistors_netlist[$current_index]. When we see a ), we increment $current_index by 1. For any other line, we just append it to the end of $resistors_netlist[$current_index].
Try this, using preg_match_all:
$text = '(
NETR5_2
R6,1
R5,2
)
(
NETR1_2
R4,2
R3,1
R3,2
R2,1
R2,2
R1,1
R1,2
)';
$chunks = explode(")(", preg_replace('/\)\W+\(/m', ')(', $text));
$result = array();
$pattern = '{([A-z0-9,]+)}';
foreach ($chunks as $row) {
preg_match_all($pattern, $row, $matches);
$result[] = $matches[1];
}
print_r($result);
3v4l.org demo
I'm not the king of regex, so you can find a better way.
The main problem are parenthesis: I don't know what are between closing and next open parenthesis ( )????( ), so first I replace every space, tab, cr or ln between, then I explode the string by )(.
I perform a foreach loop for every element of resulted array, matching every occurrence of A-z0-9, and add array of retrieved values to an empty array that, at end of foreach, will contain desired result.
Please note:
The main pattern is based on provided example: if the values contains other characters then A-z 0-9 , the regex fails.
Edit:
Replaced preliminar regex pattern with `/\)\W+\(/m`
I have a string like:
$str = "hello#$how%&*!are345You^_THere56";
I want that alphabet stores in one variable like:
hello,how,are,you,THere
Number should be stored in one variable like:
3,4,5,5,6
special characters separately:
#$%&*!^_
How can I do this?
In my opinion, the best choice for this is to use preg_split:
<?php
$str = 'hello#$how%&*!are345You^_THere56';
$words = array_filter(preg_split('/[^a-zA-Z]/', $str));
$numbers = str_split(join('', preg_split('/[^0-9]/', $str)));
$specials = str_split(join('', preg_split('/[a-zA-Z0-9]/', $str)))
print_r($words);
print_r($numbers);
print_r($specials);
By negating character classes we can filter the results how we want. The str_split an join calls are to split on character basis rather than on group basis.
Results:
Array
(
[0] => hello
[2] => how
[6] => are
[9] => You
[11] => THere
)
Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 5
[4] => 6
)
Array
(
[0] => #
[1] => $
[2] => %
[3] => &
[4] => *
[5] => !
[6] => ^
[7] => _
)
You could check for regex match.
$str = "hello#$how%&*!are345You^_THere56";
for($i=0; $i<strlen($str ); $i++)
if($str[$i] == "^[0-9]*$") {
//store numbers
}
else-if($str[$i] == "^[a-zA-Z]*$") {
// store characters
}
else {
//store special characters
}
Try this
$strs = '';
$num = '';
$sc = '';
$str = 'hello#$how%&*!are345You^_THere56';
$a = str_split($str);
$prev = '';
foreach($a as $v){
switch($v){
case is_numeric($v):
$num .= $v;
break;
case preg_match('/[a-zA-Z]/',$v):
$sc .= $v;
break;
default:
$strs .= $v;
break;
}
$prev = $v;
}
echo "<p>";
echo "<p>Strings: ".$strs."</p>";
echo "<p>Numbers: ".$num."</p>";
echo "<p>Special Characters: ".$sc."</p>";
Here's what I have to work with:
$sample = '<VAR1>TEXT</VAR1><BR><NUM1>123456789</NUM1><BR><NUM2>9</NUM2><BR><NUM3>99</NUM3>';
Here's what I would like to end up with:
$VAR1 = 'TEXT';
$NUM1 = 123456789;
$NUM2 = 9;
$NUM3 = 99;
Thank you in advance, I'm sure the solution is simple, however everything I've tried hasn't worked thus far.
This is really Really REALLY SLOPPY (I'll say that right up front) but it does work...
<?PHP
$a = '<VAR1>TEXT</VAR1><BR><NUM1>123456789</NUM1><BR><NUM2>9</NUM2><BR><NUM3>99</NUM3>';
preg_match_all("(\<.+\>(.+)\<\/.+\>)U",$a, $r);
// 4 TESTING //
echo '$r:<pre>';
print_r($r);
echo '</pre><hr>';
/*
$r Array
(
[0] => Array
(
[0] => <VAR1>TEXT</VAR1>
[1] => <BR><NUM1>123456789</NUM1>
[2] => <BR><NUM2>9</NUM2>
[3] => <BR><NUM3>99</NUM3>
)
[1] => Array
(
[0] => TEXT
[1] => <NUM1>123456789
[2] => <NUM2>9
[3] => <NUM3>99
)
)
*/
$demo = array();
$demo = array_combine ( array('VAR1'), sscanf($a, '<VAR1>%[^<]</VAR1>') );
foreach ($r[1] as $key => $value) {
if (strpos($value, '<NUM1>') !== false) {
$clean = str_replace("<NUM1>", "", $value);
$demo['NUM1'] = trim($clean);
}elseif (strpos($value, '<NUM2>') !== false) {
$clean = str_replace("<NUM2>", "", $value);
$demo['NUM2'] = trim($clean);
}elseif (strpos($value, '<NUM3>') !== false) {
$clean = str_replace("<NUM3>", "", $value);
$demo['NUM3'] = trim($clean);
}else{
}//end if
}//end foreach
// 4 TESTING //
echo 'DEMO:<pre>';
print_r($demo);
echo '</pre><hr>';
/*
$demo Array
(
[VAR1] => TEXT
[NUM1] => 123456789
[NUM2] => 9
[NUM3] => 99
)*/
?>