Regular expression for array with string
Having a little problem with RegEx, I have lines
AM.name:ASC,AMAdvanced.start:DESC,
AMAdvanced.start:DESC,AM.Genre:Action
and you need to break them into
array(0){
AM.name => ASC,
AMAdvanced.start => DESC
}
and
array(0){
AMAdvanced.start => DESC,
AM.Genre => Action
}
Any help would be fantastic as brand new to regex
+3
source to share
3 answers
There is no need here regex
.
Steps:
-
explode
comma separated,
- The loop is blown
array
-
explode
separated by colon:
- Introduce new
array
code:
$newArr = array();
foreach (explode(',', trim($str, " ,")) AS $el) {
$el = explode(':', $el);
$newArr[$el[0]] = $el[1];
}
print_r($newArr);
+2
source to share
You can split the text onto a new line character first:
var arr = data.split('\n');
then loop over your array and use the method str.replace
on your elements:
var i;
for (i = 0; i < arr.length; ++i) {
alert(arr[i].replace(/(\w+):(\w+)/g, '$1=>$2'));
}
You can also split the result for each element with ,
:
var i;
for (i = 0; i < arr.length; ++i) {
var temp=arr[i].replace(/(\w+):(\w+)/g, '$1=>$2');
alert(temp.split(','));
}
0
source to share
This will create a single array with records
<?php
$dir = array();
$strArray = array('AM.name:ASC,AMAdvanced.start:DESC','AMAdvanced.start:DESC,AM.Genre:Action');
foreach ($strArray as $i => $str) {
$el = explode(',', $str);
foreach ($el as $e) {
$dir[$i][] = explode(':',$e);
}
}
print_r($dir);
0
source to share