duancong2965
2013-09-29 12:28 阅读 56
已采纳

使用preg_split拆分复杂的字符串

Update v2

Using the code by Jerry works on most strings, but not all of them, like:

$pattern = '#^(?<tz_utf>(?:\([^)]+\)|[^-]+)+)\s+-\s+(?<tz>[^:]+)\s+:\s+(?<fr>[^/]+)\s+/\s+(?<en>[^/]+)\s+/\s+(?<ar>\S+)\s+(?<tz_dec_utf>[ⴰ-⵿ -]+)\s+(?<tz_dec>.*)$#imu';

// In this string, it doesn't validate because of no space between slash & word;
// /Alphabet => / Alphabet
// and comma in Arabic;
// ájóéHCG ,á«é¡J => ájóéHCGá«é¡J
$str4 = 'ⴰⴳⵎⵎⴰⵢ - agemmay : Alphabet, épellation /Alphabet, spelling / ájóéHCG ,á«é¡J
    ⴰⴳⵎⵎⴰⵢ - ⵓⴳⵎⵎⴰⵢ - ⵉⴳⵎⵎⴰⵢⵏ
    agemmay  – ugemmay  – igemmayen';

$str5 = 'ⴰⴷⴷⴰⴷ ⴰⵎⴰⵔⵓⵣ - addad amaruz : Etat d’annexion / Construct state / ¥ÉëdEG ádÉM
    ⴰⴷⴷⴰⴷ ⴰⵎⴰⵔⵓⵣ - ⵡⴰⴷⴷⴰⴷ ⴰⵎⴰⵔⵓⵣ
    addad  amaruz  - waddad amaruz';

$str6 = 'ⴰⴷⴷⴰⴷ ⵉⵍⴻⵍⵍⵉ - addad ilelli : Etat libre / Free state / ∫É°SQEG ádÉM
    ⴰⴷⴷⴰⴷ ⵉⵍⴻⵍⵍⵉ
    addad  ilelli';


print_r( preg_match($pattern, $str, $matches) );

Update v1

The code I'm using now matches only one portion of the whole string ($matches[1]), is it possible to extract other portions of the string using one regex?:

$pattern = '/-(.*?)\:/';    
$str1 = 'ⵜⴰⵙⵎⵙⵙⵉⵜ - tasmessit : Focalisée / Focus / QCÉÑe ⵜⴰⵙⵎⵙⵙⵉⵜ - ⵜⵙⵎⵙⵙⵉⵜ - ⵜⵉⵙⵎⵙⵙⵉⵜⵉⵏ tasmssit - tsmssit - tismssitin';
preg_match($pattern, $str1, $matches);
$arr1 = array( 
        'tz_utf'=>'ⵜⴰⵙⵎⵙⵙⵉⵜ', 
        'tz'=> $matches[1], // tasmessit
        'fr'=>'Focalisée', 
        'en'=>'Focus', 
        'ar'=>'QCÉÑe', 
        'tz_dec_utf'=>'ⵜⴰⵙⵎⵙⵙⵉⵜ - ⵜⵙⵎⵙⵙⵉⵜ - ⵜⵉⵙⵎⵙⵙⵉⵜⵉⵏ', 
        'tz_dec'=>'tasmssit - tsmssit - tismssitin'
);
print_r($matches[1]); 

Original question

For any regular expression gurus out there :)

Can you please help preg_split some strings to an array? The string value may vary and look similar to this scheme:

$str1 = 'ⵜⴰⵙⵎⵙⵙⵉⵜ - tasmessit : Focalisée / Focus / QCÉÑe ⵜⴰⵙⵎⵙⵙⵉⵜ - ⵜⵙⵎⵙⵙⵉⵜ - ⵜⵉⵙⵎⵙⵙⵉⵜⵉⵏ tasmssit - tsmssit - tismssitin';
$str2 = 'ⵜⴰⵙⵏⴰⵥⵖⵓⵕⵜ ( ⵏ-) - tasnaÇvurt (n-)  : Etymologique / Etymological / »dÉKCG ⵏ ⵜⵙⵏⴰⵥⵖⵓⵕⵜ n tesnaÇvurt';
$str3 = 'ⵜⴰⵙⵖⵓⵏⵜ ⵜⴰⵏⴰⴷⴰⵡⵜ - tasvunt tanadawt : Subordonnant / Subordinating (conjunction) / §HGQ ⵜⴰⵙⵖⵓⵏⵜ ⵜⴰⵏⴰⴷⴰⵡⵜ - ⵜⵉⵙⵖⵡⴰⵏ ⵜⵉⵏⴰⴷⴰⵡⵉⵏ tasvunt tanadawt - tisevwan tinadawin';

The correct results would be;

$arr1 = array( 
                'tz_utf'=>'ⵜⴰⵙⵎⵙⵙⵉⵜ', 
                'tz'=>'tasmessit', 
                'fr'=>'Focalisée', 
                'en'=>'Focus', 
                'ar'=>'QCÉÑe', 
                'tz_dec_utf'=>'ⵜⴰⵙⵎⵙⵙⵉⵜ - ⵜⵙⵎⵙⵙⵉⵜ - ⵜⵉⵙⵎⵙⵙⵉⵜⵉⵏ', 
                'tz_dec'=>'tasmssit - tsmssit - tismssitin'
        );
$arr2 = array( 
                'tz_utf'=>'ⵜⴰⵙⵏⴰⵥⵖⵓⵕⵜ ( ⵏ-)', 
                'tz'=>'tasnaÇvurt (n-)', 
                'fr'=>'Etymologique', 
                'en'=>'Etymological', 
                'ar'=>'»dÉKCG', 
                'tz_dec_utf'=>'ⵏ ⵜⵙⵏⴰⵥⵖⵓⵕⵜ', 
                'tz_dec'=>'n tesnaÇvur'
        );
$arr3 = array( 
                'tz_utf'=>'ⵜⴰⵙⵖⵓⵏⵜ ⵜⴰⵏⴰⴷⴰⵡⵜ', 
                'tz'=>'tasvunt tanadawt', 
                'fr'=>'Subordonnant', 
                'en'=>'Subordinating (conjunction)', 
                'ar'=>'§HGQ', 
                'tz_dec_utf'=>'ⵜⴰⵙⵖⵓⵏⵜ ⵜⴰⵏⴰⴷⴰⵡⵜ - ⵜⵉⵙⵖⵡⴰⵏ ⵜⵉⵏⴰⴷⴰⵡⵉⵏ', 
                'tz_dec'=>'tasvunt tanadawt - tisevwan tinadawin'
        );

The tz_utf are Tifinagh charcters in unicode.

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongzi5062 dongzi5062 2013-09-29 14:03

    Try using the regex:

    ~^(?<tz_utf>(?:\([^)]+\)|[^-]+)+)\s+-\s+(?<tz>[^:]+)\s+:\s+(?<fr>[^/]+)\s+/\s+(?<en>[^/]+)\s+/\s+(?<ar>\S+)\s+(?<tz_dec_utf>[ⴰ-⵿ -]+)\s+(?<tz_dec>.*)$~ui
    

    Warning, I'm not sure about the special character part as from the Armenian characters (I used a \S+ for them assuming they are single word and I used a range from this site for the characters which appear like white squares), but it's working for the sample your provided.

    regex101 demo

    点赞 评论 复制链接分享

相关推荐