-1

I have these two pieces of text

120 - 140 (cm)

and

 110 (cm)

I'd like to store the values into an array like

$array[0] = 120
$array[1] = 140

$array_2[0] = 110

How would i do this?

3
  • Do you have the 2 texts in variables ? Commented Mar 24, 2015 at 12:00
  • I'd go with regex if you're absolutely sure that every time the format will be exactly as you described it above. Commented Mar 24, 2015 at 12:00
  • Yes the text are in variables. Commented Mar 24, 2015 at 12:01

2 Answers 2

1

You could do

function numArray($str)
{
    $str = preg_replace("/[^0-9,.]/", " ", $str);
    return preg_split('/ /', $str, -1, PREG_SPLIT_NO_EMPTY);
}

$str = "120 - 140 (cm)";

$array = numArray($str);

That will return an array with only numbers in it

Sign up to request clarification or add additional context in comments.

Comments

0

Something like this?

<?php
$array = array();
$array_2 = array();

$array[0] = 120;
$array[1] = 140;

$array_2[0] = 110;
?>

2 Comments

Here you have to tell me the difference to OP's code
@Lenap OP wants to parse piece of text...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.