JavaScript split equivalent of PHP explode function
Written by admin on April 3rd, 2007 in Technology, Tips.
PHP Explode
In PHP we can easily break a long string into smaller parts by using the explode() function of PHP. In run rime, this function works like this:
$longtext=â€This is a long text stringâ€;
$brokentext=explode(†“, $longtext);
After the execution of the second command the variable $brokentext is an array such that,
$brokentext[0]=â€Thisâ€
$brokentext[1]=â€isâ€
$brokentext[2]=â€aâ€
$brokentext[3]=â€longâ€
$brokentext[4]=â€textâ€
$brokentext[5]=â€stringâ€
Javascript Split
and so on. So how do we do it in JavaScript. In JavaScript there is a split() function that achieves the same objective, although the syntax is a bit different.
var longtext=â€This is a long text stringâ€;
var brokentext=longtext.split(†“);
Now the variable brokentext has all those words.
