[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

What is php's equivalent of javascripts split()?

Posted on 2012-09-01
4
Medium Priority
?
574 Views
Last Modified: 2012-09-01
Hi, if I wanted to split something based on a certain character in javascript I would do that following:

var text = '5-bracelets';
text.split('-');

Open in new window


then I could do:

text[0];
text[1];

Open in new window


I am having trouble figuring out what php's equivalent is.  Does anyone know?
0
Comment
Question by:FairyBusiness
4 Comments
 
LVL 7

Assisted Solution

by:ziceva
ziceva earned 664 total points
ID: 38357816
Hi,

The function you are looking for is explode()

http://php.net/manual/en/function.explode.php
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 668 total points
ID: 38357824
A slightly more sophisticated version is here...
http://php.net/manual/en/function.preg-split.php

If all you need is a single character, explode() is the right tool.
0
 
LVL 8

Accepted Solution

by:
mrh14852 earned 668 total points
ID: 38357837
To work with arrays and delimited text in PHP you have several choices however the most common are implode() and explode().

<?php

// create array from comma separated string
$string = "item1,item2,item3";
$array = explode(",",$string);
print_r($array);
// Output 
// Array ( [0] => item1 [1] => item2 [2] => item3 )

// Or use other delimiters (space)
$string = "item1 item2 item3";
$array = explode(" ",$string);
print_r($array);
// Output
// Array ( [0] => item1 [1] => item2 [2] => item3 )

// (:)
$string = "This:is:a:colon:separated:string";
$array = explode(":",$string);
print_r($array);
// Output
// Array ( [0] => This [1] => is [2] => a [3] => colon [4] => separated [5] => string )

// Create string from arrays
$arr = array("item1","item2","item3");
$string = implode(",",$arr);
echo $string;
// Output
// item1,item2,item3

?>

Open in new window

0
 

Author Closing Comment

by:FairyBusiness
ID: 38358015
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question