split using Regular expressions
Moderator: General Moderators
split using Regular expressions
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.
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
Too early in the morning for regular expressions.
You sure you want to keep the quotes?
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;
}
Re: split using Regular expressions
Code: Select all
preg_match_all('#(".+?"|\w+)#', $s, $a);
print_r($a);Code: Select all
preg_match_all('#"(.+?)"|(\w+)#', $s, $a);PS: This thread is fo the RegExp section.
There are 10 types of people in this world, those who understand binary and those who don't
Re: split using Regular expressions
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.
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
What code did you use?
There are 10 types of people in this world, those who understand binary and those who don't
Re: split using Regular expressions
Code: Select all
preg_match_all('#(".+?"|\w+)#', $s, $a);
Re: split using Regular expressions
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
)
)There are 10 types of people in this world, those who understand binary and those who don't
Re: split using Regular expressions
I'm getting an input from another source ($qArray[$x]), and when i var_dump it, it gives:
And then doing RE:
I get this:
Code: Select all
string(28) ""cd dvd" recorder "Code: Select all
preg_match_all('#(".+?"|\w+)#', $qArray[$x], $qSubArray)
print "<pre>"; print_r($qSubArray); print("</pre>");
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
)
)Code: Select all
Re: split using Regular expressions
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
)
)There are 10 types of people in this world, those who understand binary and those who don't
Re: split using Regular expressions
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.
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
I only count 18, like in VladSun's code.string(28) ""cd dvd" recorder "
What does bin2hex($variable) give you?
Re: split using Regular expressions
Code: Select all
bin2hex($qArray[$x]);Re: split using Regular expressions
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
it produces this output:
when ran through my application.
Standalone, your provided code works fine.
Code: Select all
Array ( [0] => dvd [1] => cd [2] => "recorder [3] => player" [4] => tv )Standalone, your provided code works fine.
Re: split using Regular expressions
That missed out the preg's completely and went straight to the explode()...
Is you application just ignoring any PCRE functions???
Is you application just ignoring any PCRE functions???