Web Hosting Talk







View Full Version : php preg extract values from a string


cuantica
08-26-2004, 12:58 AM
Could you help me please in correcting this problem with regular expression to extract values from a string

<?
$str = "24 \"1 1/2¨\" 1.4 L.1 L.1"
// I was using this expression
$my_array = preg_split('/ /', $str, -1, PREG_SPLIT_NO_EMPTY);
?>

But it is not working for inner space in the second item
$my_array[0]=24
$my_array[1]="1
$my_array[2]=1/2"
$my_array[3]=1.4
...

And what I need to obtain
$my_array[0]=24
$my_array[1]="1 1/2"
$my_array[2]=1.4

Thanks in advance

xerohosting
08-26-2004, 06:01 AM
why not do:

$my_array = explode(" ",$str);?

cuantica
08-26-2004, 08:56 AM
Thanks but, explode introduce blank items if double space is found anywhere, like this

$my_array[0]=24
$my_array[1]=""
$my_array[2]="1 1/2"
$my_array[3]=""
$my_array[4]=1.4
$my_array[5]=""

i need the exact order in array to process it recursively

bueno
08-26-2004, 10:06 AM
To remove multiple occurences of whitespace characters in a string an convert them all into single spaces, use this:

<?

$text = preg_replace('/\s+/', ' ', $text);

?>



you may also need to try array_filter (http://www.php.net/array_filter) function

cuantica
08-26-2004, 02:12 PM
thank you guys, good idea.

cuantica
08-26-2004, 04:01 PM
I continue having trouble

here is my string as is in the file

"1 3/8''" "11" "" "" "" "" "" "" "" "" 7.9

$product = preg_replace('/\s+/', ' ', $product);
$my_array = explode(" ",$product);

And I obtain

Still get first value broken

$my_array[0] = "1
$my_array[1] = 3/8""
$my_array[2] = "11"
$my_array[3] = ""

any ideas, I´m stuck

xerohosting
08-26-2004, 04:14 PM
If you only want to join those array values with a '/' in then you could cheat:

$i = 0;
foreach($my_array as $value) {
if(strstr("/",$value) {
$new_array[($i-1)] .= $value;
} else {
$new_array[$i] = $value;
$i++;
}
}


it's pretty horrible and i'm sure there must be a nicer way, but maybe that will work?

Radchenko
08-26-2004, 04:14 PM
Reason being that you have a space at the end of the string.
I think your going about it the wrong way though, why not do something like this:
preg_match_all("/(\S+)/", $product, $my_array);

Radchenko
08-26-2004, 04:53 PM
Oh, misunderstood the question, what your saying is you want to split the string sorta like the command line splits it.

In that case, might as well just cheat and use the command line:
<?
$str = "24 \"1 1/2¨\" 1.4 L.1 L.1";
exec("perl -e 'print join(\"\n\", @ARGV)' $str", $array);
print_r($array);
?>
Returns the following:

Array
(
[0] => 24
[1] => 1 1/2¨
[2] => 1.4
[3] => L.1
[4] => L.1
)

Its a cheap hack, but it ends up giving you what you want.
You may also want to be careful with it to make sure to escape $str to make sure nothing isn't run by 'accident'

xerohosting
08-26-2004, 04:58 PM
Very nice Radchenko!

Note that that also requires your host to allow shell functions to be executed so if you're getting no output then its probably because you're not allowed to exec().

cuantica
08-26-2004, 05:12 PM
Thanks to all,

I have tried many ways and this seems to work

$producto = preg_replace('/\s+/', ' ', $producto);
$producto = preg_replace('/"(\d+) (\d+\/\d+.+)"/', '"\\1-\\2"', $producto);
$nombres = explode(' ', $producto);

this only works for strings like the one causing the problem \"1 3/4\" and it is changing the layout of the string but seems to be enough for now.