Page 1 of 2
split using Regular expressions
Posted: Wed Nov 19, 2008 10:13 am
by sbutt
Hi there,
I need a little help regarding regular expressions in php4.
I have a string e.g. $str = 'dvd cd "recorder player" tv';
I want to split this string into pieces using space as a delimiter but a phrase like "recorder player" i dont want to split further.
So the outcome i want should be
[0] = dvd
[1] = cd
[2] = "recorder player"
[3] = tv
I have tried to implement without RE, but it is getting complicated, so i would appreciate if some could help me building the method using RE?
Thanks.
Re: split using Regular expressions
Posted: Wed Nov 19, 2008 11:31 am
by requinix
Too early in the morning for regular expressions.
Code: Select all
$str = 'dvd cd "recorder player" tv';
$split = explode('"', $str);
$parts = array(); $i = 0;
foreach ($split as $s) {
$i = 1 - $i;
$s = ($i ? explode(" ", trim($s)) : array("\"$s\""));
foreach ($s as $t) $parts[] = $t;
}
You sure you want to keep the quotes?
Re: split using Regular expressions
Posted: Wed Nov 19, 2008 12:01 pm
by VladSun
Code: Select all
preg_match_all('#(".+?"|\w+)#', $s, $a);
print_r($a);
or
Code: Select all
preg_match_all('#"(.+?)"|(\w+)#', $s, $a);
@GeertDD

How one should exlcude the " from my first regexp?
PS: This thread is fo the RegExp section.
Re: split using Regular expressions
Posted: Thu Nov 20, 2008 8:38 am
by sbutt
well really appreciated for you help, but perhaps i still have a problem.
When i take a var_dump of my input string ("cd dvd" recorder), i get the following:
string(28) ""cd dvd" recorder "
Array
(
[0] => quot
[1] => cd
[2] => dvd
[3] => quot
[4] => recorder
)
It should be:
Array
(
[0] => "cd dvd"
[2] => recorder
)
Or
Array
(
[0] => quotcd dvdquot
[2] => recorder
)
thanks.
Re: split using Regular expressions
Posted: Thu Nov 20, 2008 9:15 am
by VladSun
What code did you use?
Re: split using Regular expressions
Posted: Thu Nov 20, 2008 9:31 am
by sbutt
Code: Select all
preg_match_all('#(".+?"|\w+)#', $s, $a);
Re: split using Regular expressions
Posted: Thu Nov 20, 2008 9:39 am
by VladSun
sbutt wrote:Code: Select all
preg_match_all('#(".+?"|\w+)#', $s, $a);
Code: Select all
$s = '""cd dvd" recorder"';
preg_match_all('#(".+?"|\w+)#', $s, $a);
print_r($a);
=>
Code: Select all
Array
(
[0] => Array
(
[0] => ""cd dvd"
[1] => recorder
)
[1] => Array
(
[0] => ""cd dvd"
[1] => recorder
)
)
???
Re: split using Regular expressions
Posted: Thu Nov 20, 2008 10:00 am
by sbutt
I'm getting an input from another source ($qArray[$x]), and when i var_dump it, it gives:
And then doing RE:
Code: Select all
preg_match_all('#(".+?"|\w+)#', $qArray[$x], $qSubArray)
print "<pre>"; print_r($qSubArray); print("</pre>");
I get this:
Code: Select all
Array
(
[0] => Array
(
[0] => quot
[1] => cd
[2] => dvd
[3] => quot
[4] => recorder
)
[1] => Array
(
[0] => quot
[1] => cd
[2] => dvd
[3] => quot
[4] => recorder
)
)
Re: split using Regular expressions
Posted: Thu Nov 20, 2008 10:04 am
by VladSun
Code: Select all
<pre>
<?php
$x = 1;
$qArray[$x] = '"cd dvd" recorder ';
var_dump($qArray[$x]);
preg_match_all('#(".+?"|\w+)#', $qArray[$x], $qSubArray);
print_r($qSubArray);
?>
</pre>
=>
Code: Select all
string(18) ""cd dvd" recorder "
Array
(
[0] => Array
(
[0] => "cd dvd"
[1] => recorder
)
[1] => Array
(
[0] => "cd dvd"
[1] => recorder
)
)
Maybe an encoding problem ...
Re: split using Regular expressions
Posted: Thu Nov 20, 2008 10:48 am
by sbutt
encoding problem...could be but how can we resolve that
Actually i'm running the code on php4, but i have tried to run it on php5 as well but to no avail.
When i run your example alone, it works fine.
Re: split using Regular expressions
Posted: Thu Nov 20, 2008 3:50 pm
by requinix
string(28) ""cd dvd" recorder "
I only count 18, like in VladSun's code.
What does bin2hex($variable) give you?
Re: split using Regular expressions
Posted: Fri Nov 21, 2008 3:37 am
by sbutt
Actually the method returns nothing.
Re: split using Regular expressions
Posted: Fri Nov 21, 2008 4:30 am
by dhrosti
This function doesn't return it all in order, but it does split it up...
Code: Select all
function custom_split($str) {
preg_match_all('/"([\w\d\s]*)"/is', $str, $quotes);
$str = preg_replace('/("[\w\d\s]*")/is', '', $str);
$matches = explode(' ', $str);
$words = array_filter(array_merge($quotes[1], $matches));
return $words;
}
$parts = custom_split('dvd cd "recorder player" tv');
print_r($parts);
Re: split using Regular expressions
Posted: Fri Nov 21, 2008 4:42 am
by sbutt
it produces this output:
Code: Select all
Array ( [0] => dvd [1] => cd [2] => "recorder [3] => player" [4] => tv )
when ran through my application.
Standalone, your provided code works fine.
Re: split using Regular expressions
Posted: Fri Nov 21, 2008 4:47 am
by dhrosti
That missed out the preg's completely and went straight to the explode()...
Is you application just ignoring any PCRE functions???