We help IT Professionals succeed at work.

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

FairyBusiness
on
Medium Priority
215 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?
Comment
Watch Question

Juan OcasioContinuous Process Improvement Lead
CERTIFIED EXPERT

Commented:
Check out the preg_split function:

http://php.net/manual/en/function.preg-split.php
Greg AlexanderLead Developer

Commented:
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

Lead Developer
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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!
Greg AlexanderLead Developer

Commented:
sorry!  Thanks for the points
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.