Regular Expression Help
Hi
It should be easy to extract digits and characters, some thing like.
//String Numbers[]
//String Mulitpliers[]
//S is the string that contains the sequence of digits and operators.
$i=0;
$n=0;
while($S[i] != null)
{
switch($S[i])
{
case '0','1','2','3','4','5','6','7','8','9': $Numbers[n] =
$Numbers[n] + $S[i];
case 'x','*': $Mulitpliers[n] = $Mulitpliers[n] + $S[i];
case ' ': $n = $n+1;
}
$i = $i+1;
}
Lars
"Hoss" <komeatshield@yahoo.com> skrev i meddelandet
news:c9c2f35a-9585-46ec-aa76-36bb0cf05875@h11g2000prf.googlegroups.com...
You could use preg_match_all with the following regular expression (it
just extracts the numbers from a string and puts them in an array)
(\d+)
preg_match_all('/(\d+)/', '23x 8x 15',$matches);
Are you only interested in extracting the numbers, or is it important
to get the 'x' too? If it is dimensions, then it will always have
three (non-negative) numbers in some form, the expression I posted
should work (UNLESS they enter something like '12.1 x5.2 x 5')
You could try
preg_match_all('/([0-9.]+)/', '23.5x 8x 15',$matches);
which will work for decimals, but it will fail if your string has
periods in it that are not decimal points '12.5 x. 1.2. x.'
|