2

First of all, I tried to search it on SO but couldn't find a solution. I have a string

var str = 'a:5:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";i:4;s:1:"5";}';

I want to grab all numbers between " " as an array in javascript. I tried this suggested on one of the SO questions.

console.log(str.split(/[""]/));

but this outputs

["a:5{i:0;s:1:", "1", ";i:1;:", "2", ";i:2;s:1:", "7", ";i:3;s:1:", "4", ";i:4;s:1:", "5", ";}"]

which is not exact.

Actually that above string is an array of PHP stored in MemcacheD and I am retrieving it in Node.JS and that's why I can't use json_encode or so. I am not good at Regex. So Experts please show some shine on it.

The result of above string should be an array or string like this

str = "1,2,3,4,5"; 

or an array like

array = [1,2,3,4,5];
Matt
  • 20,108
  • 1
  • 57
  • 70
  • That looks a bit like a `bencoded` string (but it's not)? What is the encoding? – Xotic750 Feb 27 '14 at 23:45
  • I don't know. I just got this string in Node.js using its extension Memcached. This is an array stored by PHP in memcached. –  Feb 27 '14 at 23:47
  • That would be really useful to know, as there may be a decoder available in javascript.Which would be much better than a RegEx. – Xotic750 Feb 27 '14 at 23:48
  • I wish there was. But currently I think I have to go with regex get it like 1,2,3,4,5 and then split it into an array. –  Feb 27 '14 at 23:50
  • `memcached` is `Python` thing isn't it? – Xotic750 Feb 27 '14 at 23:51
  • 2
    that's a php array generated with [serialize](http://us.php.net/manual/en/function.serialize.php) and memcached is an in-memory cache, you can connect to it from anything. – OneOfOne Feb 27 '14 at 23:53
  • Great! Then there is probably a `php.js` version of `unserialize` then. – Xotic750 Feb 27 '14 at 23:55
  • Thanks to all of guys. I think Matt has suggested good one –  Feb 27 '14 at 23:57

3 Answers3

3
str.match(/"\d+"/g).join().replace(/"/g,'')
// "1,2,3,4,5"
Matt
  • 20,108
  • 1
  • 57
  • 70
  • Matt is it possible to append a keyword like **user** to each of result like the output should be "1user,2user,3user,4user,5user"? please see if possible –  Feb 28 '14 at 12:18
  • 1
    @CodeDevil `str.match(/"\d+"/g).join().replace(/"(\d+)"/g,"$1user")` – Matt Feb 28 '14 at 15:38
  • why your regex does not work if there are alphabets in string like this 'a:7:{i:0;s:1:"1";i:1;s:12:"John Smith";i:2;s:19:"My Life Begins Here";i:3;s:31:"This is my .Picture.jpg";i:4;s:10:"1988-07-26";}' even though they just like previous one in between "" e.g: "John Smith"? –  Feb 28 '14 at 22:57
  • Please @Matt have a look at this issue –  Feb 28 '14 at 22:59
  • @CodeDevil Two reasons: 1) Your question said 'I want to grab all **numbers** between " "' 2) `\d` matches only digits. If you want to generalize, this would appropriately be a separate question as the strategy would be different. (If it were a simple change, I'd be happy to share it.) – Matt Feb 28 '14 at 23:00
  • well What if I update my question? because otherwise others will down vote and close my question. I tried removing \d but that didn't work. –  Feb 28 '14 at 23:01
  • @CodeDevil I don't understand - if you open up a completely separate StackOverflow question, why would others downvote and close it? – Matt Feb 28 '14 at 23:02
  • Ok let me make an other question. –  Feb 28 '14 at 23:03
  • see this http://stackoverflow.com/questions/22107547/get-substring-between-two-characters-in-javascript –  Feb 28 '14 at 23:10
0
function match_all(str, re) {
    var ret = [], v;
    while(v = re.exec(str)) {
        ret.push(v[1]);
    }
    return ret;
}
var str = 'a:5:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";i:4;s:1:"5";}';
var numbers = match_all(str, /"(\d+)"/g);
console.log(numbers);
//to convert the result to ints
console.log(numbers.map(function(v) { return +v; }));
OneOfOne
  • 95,033
  • 20
  • 184
  • 185
0

So, this is a PHP serialized string. There is a version of unserialize available for javascript in the php.js project.

So using that function.

Javascript

var str = 'a:5:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";i:4;s:1:"5";}';

console.log(unserialize(str));

Output

["1", "2", "3", "4", "5"] 

On jsFiddle

And if you are using nodejs then you could also consider a module like php-unserialize

Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • Oh man that's good but don't you think using regex would be a better option instead of such long code? –  Feb 28 '14 at 00:02
  • IMO, I would say nope. – Xotic750 Feb 28 '14 at 00:04
  • Great Xotic, Thanks for suggesting module –  Feb 28 '14 at 00:10
  • No problem. The nodejs module basically uses the function provided on `php.js` just wrapped as a module. :) – Xotic750 Feb 28 '14 at 00:15
  • I tried your suggested jsFiddle, but it fails when handling substrings with alphabets like this 'a:7:{i:0;s:1:"1";i:1;s:12:"John Smith";i:2;s:19:"My Life Begins Here";i:3;s:31:"This is my .Picture.jpg";i:4;s:10:"1988-07-26";}'; Any solution? –  Feb 28 '14 at 23:45
  • I finally went with php-unserialize module. Thanks for your suggestion –  Mar 01 '14 at 00:25
  • Because the serialized data that you provided is incorrect, it should be `'a:5:{i:0;s:1:"1";i:1;s:10:"John Smith";i:2;s:19:"My Life Begins Here";i:3;s:23:"This is my .Picture.jpg";i:4;s:10:"1988-07-26";}';` and then it gives an output `["1", "John Smith", "My Life Begins Here", "This is my .Picture.jpg", "1988-07-26"]` as can be seen http://jsfiddle.net/Xotic750/7hL4h/ – Xotic750 Mar 01 '14 at 02:01
  • Hey Matt it's strange that everything is working great but only such array it hungs **a:5:{UID:s:1:"1";EM:s:19:"johnsmith@yahoo.com";PSW:s:40:"123a5123a5123a5123a5123a5123a5123a5123a5";NAME:s:11:"John Smith";PIC:s:31:"123a5123a5123a5123a5123a512.jpg";}** why? I used your provided Fiddle and php-unserialize module as well but both give exception –  Mar 02 '14 at 13:32
  • it's php session data stored in memcached. I do notice there is a difference but can't that be handled? –  Mar 02 '14 at 13:34
  • Try your string online at [http://www.functions-online.com/](http://www.functions-online.com/unserialize.html), or at [http://blog.tanist.co.uk/](http://blog.tanist.co.uk/files/unserialize/), or at [http://i-tools.org/](http://i-tools.org/unserialize). Your string seems to have a problem again.There are even more available online if you google for them. – Xotic750 Mar 02 '14 at 14:15
  • If it is session data then `php-unserialize` proveds a special method for that format called `unserializeSession` which performs PHP's [`session_decode`](http://se2.php.net/session_decode) – Xotic750 Mar 02 '14 at 14:23
  • really? Let me check it please –  Mar 02 '14 at 14:31
  • And I believe there is a phpjs version too, https://github.com/vianneyb/phpjs/blob/master/functions/var/session_decode.js But this is a link to a forked project, you would have to investigate where it lives in the main project. – Xotic750 Mar 02 '14 at 14:35
  • Thanks Matt this worked great. Thanks for your kind support man –  Mar 02 '14 at 15:05
  • No problem and it's `Xotic750` and not `Matt` :P – Xotic750 Mar 02 '14 at 15:09
  • OH really sorry man actually had another discussion with someone Matt :p –  Mar 02 '14 at 15:30