split using Regular expressions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

sbutt
Forum Newbie
Posts: 11
Joined: Mon Jun 09, 2008 5:59 am

split using Regular expressions

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: split using Regular expressions

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: split using Regular expressions

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
sbutt
Forum Newbie
Posts: 11
Joined: Mon Jun 09, 2008 5:59 am

Re: split using Regular expressions

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: split using Regular expressions

Post by VladSun »

What code did you use?
There are 10 types of people in this world, those who understand binary and those who don't
sbutt
Forum Newbie
Posts: 11
Joined: Mon Jun 09, 2008 5:59 am

Re: split using Regular expressions

Post by sbutt »

Code: Select all

 
preg_match_all('#(".+?"|\w+)#', $s, $a);
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: split using Regular expressions

Post 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
        )
 
)
???
There are 10 types of people in this world, those who understand binary and those who don't
sbutt
Forum Newbie
Posts: 11
Joined: Mon Jun 09, 2008 5:59 am

Re: split using Regular expressions

Post by sbutt »

I'm getting an input from another source ($qArray[$x]), and when i var_dump it, it gives:

Code: Select all

string(28) ""cd dvd" recorder "
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
        )

)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: split using Regular expressions

Post 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 ...
There are 10 types of people in this world, those who understand binary and those who don't
sbutt
Forum Newbie
Posts: 11
Joined: Mon Jun 09, 2008 5:59 am

Re: split using Regular expressions

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: split using Regular expressions

Post by requinix »

string(28) ""cd dvd" recorder "
I only count 18, like in VladSun's code.

What does bin2hex($variable) give you?
sbutt
Forum Newbie
Posts: 11
Joined: Mon Jun 09, 2008 5:59 am

Re: split using Regular expressions

Post by sbutt »

Code: Select all

bin2hex($qArray[$x]);
Actually the method returns nothing.
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Re: split using Regular expressions

Post 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);
sbutt
Forum Newbie
Posts: 11
Joined: Mon Jun 09, 2008 5:59 am

Re: split using Regular expressions

Post 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.
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Re: split using Regular expressions

Post by dhrosti »

That missed out the preg's completely and went straight to the explode()...

Is you application just ignoring any PCRE functions???
Post Reply