Jump to content

Parsing a string?


figo2476

Recommended Posts

Hi:

 

I have this question. Something like article:"The winter" will become key & value like article=>"The winter"

Notice the ":". If it doesn't have ":", then it may be Example 1.

 

I was thinking if I am able to replace the space inside the double quotes, then I will be able to explode the string

with space,  but how can I replace the spaces within the double quotes? I guess regular expression will be helpful....

Is there any hint, anyone?

 

Example 1:

Input -  "You are" "the best" digg hello

Output -  array( "normal" => array("You are", "the best", "digg", "hello") )

 

Example 2:

Input - song:"The best year" writer:jason

Output - array("song"=>"The best year", "writer"=>"jason")

 

Example 3:

Input - article:"The winter" bruce lee

Output - array( "article"=>"The winter", "normal"=>array("bruce", "lee") )

 

Example 4:

Input - software:linux linux father title:"open source"

Output - array( "software"=>linux, "title"=>"open source", "normal"=>array("linux", "father") )

 

 

 

Link to comment
Share on other sites

Here's my version, in regex

 

<pre><?php

error_reporting( E_ALL );

$expr = <<<REGEX
%
(?[^:\s]++)?		# Match 'whatever:' 0 or 1 times - Group 1

(			# Begin Group 2
  (?:"[^"]++")		# Match "string with spaces"
   |			# OR
  (?:[^\s]++)		# Match anythingWithoutWhitespace
)			# End Group 2
%mx
REGEX;

$str = <<<STRING
"You are" "the best" digg hello
song:"The best year" writer:jason
article:"The winter" bruce lee
software:linux linux father title:"open source"
STRING;

preg_match_all( $expr, $str, $matches, PREG_SET_ORDER );

$arr = array();
foreach( $matches as $match ) {

if( !empty($match[1]) )
	$arr[ $match[1] ] = trim( $match[2], '"' );
else
	$arr['normal'][] = trim( $match[2], '"' );

}

print_r( $arr );

?></pre>

 

Outputs

 

Array
(
    [normal] => Array
        (
            [0] => You are
            [1] => the best
            [2] => digg
            [3] => hello
            [4] => bruce
            [5] => lee
            [6] => linux
            [7] => father
        )

    [song] => The best year
    [writer] => jason
    [article] => The winter
    [software] => linux
    [title] => open source
)

Link to comment
Share on other sites

Hi:

I am not very familiar with regular expression. e.g. (?:([^:\s]++):)

 

From my understanding, e.g. whatever: will "whatever" matches ?, and ":" will match : (That is not correct, right? Because there is another ":" at the end)

 

But if I ignore the "?:" at the beginning, I am able to understand. It just try to match "whatever:".

 

Also I am not sure "++", why no "+" only?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.