Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Equivalent to PHP Explode()

I have this string:

0000000020C90037:TEMP:data

I need this string:

TEMP:data.

With PHP I would do this:

$str = '0000000020C90037:TEMP:data'; $arr = explode(':', $str); $var = $arr[1].':'.$arr[2]; 

How do I effectively explode a string in JavaScript the way it works in PHP?

like image 545
Doug Molineux Avatar asked Dec 22 '10 22:12

Doug Molineux


People also ask

What is explode in JavaScript?

If you want to explode or split a string from a certain character or separator you can use the JavaScript split() method. The following example will show you how to split a string at each blank space. The returned value will be an array, containing the splitted values.

What does explode () do in PHP?

The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string. Note: This function is binary-safe.

What is difference between explode () or implode () in PHP?

PHP Explode function breaks a string into an array. PHP Implode function returns a string from an array.

What would implode explode string )) do?

PHP implode() and explode() The implode() function takes an array, joins it with the given string, and returns the joined string. The explode() function takes a string, splits it by specified string, and returns an array.


1 Answers

This is a direct conversion from your PHP code:

//Loading the variable var mystr = '0000000020C90037:TEMP:data';  //Splitting it with : as the separator var myarr = mystr.split(":");  //Then read the values from the array where 0 is the first //Since we skipped the first element in the array, we start at 1 var myvar = myarr[1] + ":" + myarr[2];  // Show the resulting value console.log(myvar); // 'TEMP:data' 
like image 115
John Hartsock Avatar answered Oct 16 '22 18:10

John Hartsock