Jack Hales
Jack Hales

Reputation: 1654

jQuery explode string like PHP

So I was looking on some SO questions and the jQuery area to try to use the similar feature in PHP using explode which will just explode a string given into it's pieces from a type of substring.

This is the code I used:

var users = // AJAX CALL TO GET USERS
var usersArr = users.split();

I looked at the w3 tutorial for splitting strings and this was the JavaScript type, and even that didn't work.

Error message:

index.js:45 Uncaught TypeError: users.split is not a function

Upvotes: 2

Views: 10625

Answers (2)

pankaj
pankaj

Reputation: 1914

just like PHP you can do but with different method.

var str = "How are you doing today?"
var res =  str.split(" ");

access this way : "String :" + res[0]+" "+res[1]+" "+res[4]+" "

output : How are today?

Upvotes: 7

ADreNaLiNe-DJ
ADreNaLiNe-DJ

Reputation: 4919

Since Ajax is asynchronous, you have to set users variable inside the success callback of the Ajax call and then split the string.

It can be don like this.

$.ajax({
    url: "url-to-the-page",
    success: function(data) {
        var users = data;
        var usersArr = users.split(";"); // if semicolon is the separator.
    }
});

Upvotes: 2

Related Questions