stack
stack

Reputation: 10228

How to Separate a string to multiple parts

I have some Persian text (direction rlt) that I want to separate them.

Example:

$str =" کامپیوتر : وسیله ی الکتریکی است 1.ماوس 2.کیبورد
       و مانیتور 3. کیس
چاپگر: وسیله ای است برای پرینت بر روی معمولا کاغذ
موبایل : نوعی تلفن است به صورت سیار و بی سیم
که جدیدا خیلی هم رایج شده است
و اکثر انسان ها دارند
خانه : محلی برای زندگی است. 1. حیوانات 2. انواع انسان ها
برای خود خانه می سازند. ";

I want this output:

{
    arr[
       {
         word: "کامپیوتر",
         mean: "وسیله ی الکتریکی است 1.ماوس 2.کیبورد و مانیتور 3. کیس"  
       },

       {
        word: "چاپگر",
        mean: "وسیله ای است برای پرینت بر روی معمولا کاغذ"
       },

       {
        word: "موبایل",
        mean: "نوعی تلفن است به صورت سیار و بی سیم که جدیدا خیلی هم رایج شده است و اکثر انسان ها دارند"
       },

       {
        word: "خانه",
        mean: "محلی برای زندگی است. 1. حیوانات 2. انواع انسان ها برای خود خانه می سازند."
       }
      ]
}

Well, I think I can't just use explode(":", $str). Because the mean of word is not contestant, it is sometimes in several lines. I think I need to regex. So how can I do that?


Edit: An English example:

$str = "apple : it is a fruit
       computer : 1.an electronic device for storing and 
        processing data typically in binary form 2. according to
        instructionsgiven to it in a variable program"
        wall: a continuous vertical brick or stone structure
        that encloses or divides an area of land. 1. on the
       wall 2. brick wall 3. climbing wall";

I want this output:

{
    arr[
       {
         word: "apple",
         mean: "it is a fruit"  
       },

       {
        word: "computer",
        mean: "1.an electronic device for storing and processing data typically in binary form 2. according to instructionsgiven to it in a variable program"
       },

       {
        word: "wall",
        mean: "a continuous vertical brick or stone structure that encloses or divides an area of land. 1. on the wall 2. brick wall 3. climbing wall"
       }
      ]
}

Upvotes: 0

Views: 99

Answers (2)

user557597
user557597

Reputation:

This is a better way to do it.

This works but you have an extra step to do trimming of newlines in the meaning.

Just sit in a find loop. When you get a match, just run this replace on
the contents of meaning - group 2.
Then just store the results in an array.

Find: \s*\r?\n\s*
Replace: " "

Main regex:

(?m)^\h*([^:\r\n]*?)\h*:(.*(?:\s*^(?!\h*[^:\r\n]*?\h*:).*)*)

Formatted and tested:

 (?m)
 ^ 
 \h* 
 ( [^:\r\n]*? )                # (1) Word
 \h* :
 (                             # (2 start) Meaning
      .*  
      (?:
           \s* 
           ^          
           (?!
                \h* [^:\r\n]*? \h* :
           )
           .* 
      )*
 )                             # (2 end)

Output:

 **  Grp 1 -  ( pos 1 , len 8 ) 
کامپیوتر  
 **  Grp 2 -  ( pos 11 , len 62 ) 
 وسیله ی الکتریکی است 1.ماوس 2.کیبورد
       و مانیتور 3. کیس  

---------------------

 **  Grp 1 -  ( pos 75 , len 5 ) 
چاپگر  
 **  Grp 2 -  ( pos 81 , len 43 ) 
 وسیله ای است برای پرینت بر روی معمولا کاغذ  

---------------------

 **  Grp 1 -  ( pos 126 , len 6 ) 
موبایل  
 **  Grp 2 -  ( pos 134 , len 90 ) 
 نوعی تلفن است به صورت سیار و بی سیم
که جدیدا خیلی هم رایج شده است
و اکثر انسان ها دارند  

---------------------

 **  Grp 1 -  ( pos 226 , len 4 ) 
خانه  
 **  Grp 2 -  ( pos 232 , len 76 ) 
 محلی برای زندگی است. 1. حیوانات 2. انواع انسان ها
برای خود خانه می سازند.   

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626799

You can use the following regex:

'~\h*(?<term>[^:\n]*?)\s*:\s*(?<mean>(?:(?!\n\h*[^\n:]*:).)*)~us'

See regex demo

I am using the named capture groups so that you could access them easier later on. Note that you need /u modifier to work with Unicode strings in PHP regex!

The regex matches:

  • \h* - 0 or more horizontal whitespace
  • (?<term>[^:\n]*) - Group 1 named "term" that matches 0 or more characters other than : and \n
  • \s*:\s* - 0 or more whitespaces followed by : and zero or more whitespaces
  • (?<mean>(?:(?!\n\h*[^\n:]*:).)*) - Group 2 named "mean" that matches any characters (since I am using /s modifier) that are not starting a sequence like spaces+term+:. This (?:(?!...).)* construct is called a tempered greedy token. You can unroll this as (?<mean>[^\n]*(?:\n(?!\h*[^\n:]*:)[^\n]*)*) for better performance (192 steps vs. 1226).

Use the regex with the preg_match_all rather than with preg_replace since you need an array:

$str =" کامپیوتر : وسیله ی الکتریکی است 1.ماوس 2.کیبورد
       و مانیتور 3. کیس
چاپگر: وسیله ای است برای پرینت بر روی معمولا کاغذ
موبایل : نوعی تلفن است به صورت سیار و بی سیم
که جدیدا خیلی هم رایج شده است
و اکثر انسان ها دارند
خانه : محلی برای زندگی است. 1. حیوانات 2. انواع انسان ها
برای خود خانه می سازند. ";
preg_match_all('~\h*(?<term>[^:\n]*?)\s*:\s*(?<mean>(?:(?!\n\h*[^\n:]*:).)*)~us', $str, $m, PREG_SET_ORDER);
print_r($m);

See the code demo.

Upvotes: 2

Related Questions