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
https://forums.phpfreaks.com/topic/125471-parsing-a-string/
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
https://forums.phpfreaks.com/topic/125471-parsing-a-string/#findComment-648688
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
https://forums.phpfreaks.com/topic/125471-parsing-a-string/#findComment-649201
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.