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

x
?
Solved

How to specify where I want to split a string in an array?

Posted on 2011-04-26
5
Medium Priority
?
199 Views
Last Modified: 2012-05-11
Hi, in one of my functions I am pulling an int value from a database table and I am turning that int 'string' into an array:

$materials = str_split($materials);

so if I had 8970989, each number would become a variable.

but sometimes I don't just want single digits but double digits.  So can I tell it to parse at a period?  Like this:

8.97.09.8.9

so that it would return variables of whats in between the periods.  Not sure that makes sense. . anyone got any suggestions?
0
Comment
Question by:FairyBusiness
  • 3
5 Comments
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 35472258
Check out the preg_split function:

http://php.net/manual/en/function.preg-split.php
0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35472260
Maybe something like this?:
<?
$sting = "8.97.09.8.9";

$new_strings = explode(".",$string);

foreach($new_stings as $value){
	echo "$value<br>";
}
?>

Open in new window

0
 
LVL 19

Accepted Solution

by:
Greg Alexander earned 2000 total points
ID: 35472264
This rather... sorry I had a typo
<?
$sting = "8.97.09.8.9";

$new_strings = explode(".",$string);

foreach($new_strings as $value){
	echo "$value<br>";
}
?>

Open in new window

0
 

Author Comment

by:FairyBusiness
ID: 35472322
You had a couple of typos actually lol

<?
$string = "8.97.09.8.9";

$new_strings = explode(".",$string);

foreach($new_strings as $value){
	echo $value ."<br />";
}
?>

Open in new window


But it works.  The preg_split looks very complicated!  Anyways, thanks for the help!
0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35472328
sorry!  Thanks for the points
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

830 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