简体   繁体   English

每次我在PHP中执行正则表达式拆分时,PHP返回的数组中的第一个和最后一个字符串都显示为空

[英]Every time I do a regular expression splitting in PHP the first and last string in the array PHP returns appears empty

I am making a platform to learn Japanese and I have over 2000 hiraganas, katakanas and kanjis and their respective romajis (they are the sound they make when you pronounce them) that I want to insert into a MySQL database. 我正在搭建一个学习日语的平台,我想将2000多种hiraganas,katakanas和kanjis及其各自的romajis(它们是您发音时发出的声音)插入到MySQL数据库中。 but the problem is that I have them in a string like this (this are just the katakanas, imagine now over 2000 Asian characters more!): 但是问题是我将它们放在这样的字符串中(这只是片假名,请想象现在超过2000个亚洲字符!):

    $string = "a    ア   ka  カ   sa  サ   ta  タ   na  ナ
    i   イ   ki  キ   shi シ   chi チ   ni  ニ
    u   ウ   ku  ク   su  ス   tsu ツ   nu  ヌ
    e   エ   ke  ケ   se  セ   te  テ   ne  ネ
    o   オ   ko  コ   so  ソ   to  ト   no  ノ
    ha  ハ   ma  マ   ya  ヤ   ra  ラ   wa  ワ
    hi  ヒ   mi  ミ           ri  リ   (wi)    ヰ
    fu  フ   mu  ム   yu  ユ   ru  ル   n   ン
    he  ヘ   me  メ           re  レ   (we)    ヱ
    ho  ホ   mo  モ   yo  ヨ   ro  ロ   (w)o    ヲ   ga  ガ   za  ザ   da  ダ   ba  バ   pa  パ
    gi  ギ   ji  ジ   ji  ヂ   bi  ビ   pi  ピ
    gu  グ   zu  ズ   zu  ヅ   bu  ブ   pu  プ
    ge  ゲ   ze  ゼ   de  デ   be  ベ   pe  ペ
    go  ゴ   zo  ゾ   do  ド   bo  ボ   po  ポ

    kya キャ  sha シャ  cha チャ  hya ヒャ  pya ピャ
    kyu キュ  shu シュ  chu チュ  hyu ヒュ  pyu ピュ
    kyo キョ  sho ショ  cho チョ  hyo ヒョ  pyo ピョ

    gya ギャ  ja  ジャ  nya ニャ  bya ビャ  mya ミャ
    gya ギュ  ju  ジュ  nyu ニュ  byu ビュ  my  ミュ
    gyo ギョ  jo  ジョ  nyo ニョ  byo ビョ  myo ミョ
    rya リャ  ryu リュ  ryu リョ  (ja)    ヂャ  (ju)    ヂュ";

So far I could split them between Asian characters and romajis, but with it also split tabulations, and there are blank characters in the first and last part of the array. 到目前为止,我可以将它们分成亚洲字符和罗马字母,但是也可以将列表分开,并且在数组的第一部分和最后一部分中都有空白字符。

You should consider exploding the string into an array, using the tab as delimiter. 您应该考虑使用制表符作为分隔符,将字符串分解为数组。 Once you have the array you can loop through it separating out the characters. 一旦有了数组,就可以遍历整个数组,以分离出字符。 That's how I'd start. 我就是这样开始的。

php.net is going to be a great resource for you, check out the explode() function . php.net将是您的绝佳资源,请查看explode()函数

Try 尝试

preg_match_all('/(\S+)\s/+(\S+)\s*/', $string, $matches, PREG_SET_ORDER);
print_r($matches);

This searches for the pattern: letters, whitespace, letters, whitespace - and then repeating this pattern for the entire string. 这将搜索模式:字母,空格,字母,空格-然后对整个字符串重复此模式。

I'm not sure what kind of output you want from your regex, but if you use this you'll get a 2D array with each sub array containing two elements (Every time it reads two words it adds a new array to the main array for the next two). 我不确定您要从正则表达式中获得哪种输出,但是如果使用它,您将获得一个2D数组,每个子数组包含两个元素(每次读取两个单词时,它将向主数组添加一个新数组接下来的两个)。 It also strips the parenthesis from ja and ju . 它还从jaju括号。 Let me know if you need to keep those. 让我知道您是否需要保留这些。 It's also very fragile (If there are an odd number of words in $string it will cause a PHP E_NOTICE warning. Let me know if you need that changed: 它也非常脆弱(如果$string的单词数量奇数,将导致PHP E_NOTICE警告。如果需要更改,请告诉我:

$arr = array();
preg_match_all('/(?<=^|\s)\S+(?=\s|$)/mu', $string, $arr);
$count = (int)(count($arr[0])/2);
for($i = 0; $i < $count; $i++)
    $arr[0][$i] = array($arr[0][$i*2], $arr[0][$i*2+1]);
$arr = array_slice($arr[0], 0, $count);

echo $arr[0][0].': '.$arr[0][1];      // Outputs "a: ア"
echo $arr[107][0].': '.$arr[107][1];  // Outputs "ju: ヂュ"

Try this: 尝试这个:

<?php
    $string =
   "a    ア   ka  カ   sa  サ   ta  タ   na  ナ
    ...";
// |<-----------------------GRP#0------------------------>| // |GRP#01| |<--------------GRP#02-------------->| // |<-GRP#03->| // romans spaces non-spaces ignored-spaces '('romans')' opt-sapces preg_match_all('/([a-z]+)[ \n\r\t]+([^ \n\r\t]+(?:[ \n\r\t]+)(([a-z]+))?)[ \n\r\t]*/', $string, $matches, PREG_SET_ORDER); print_r($matches);

You should get an array of 103 elements and the last element should look like this: 您应该获得一个包含103个元素的数组,最后一个元素应如下所示:

Array
(
    [0] => ryu リョ  (ja)    
    [1] => ryu
    [2] => リョ  (ja)
    [3] => (ja)
)

I think this is self explanatory, if not let me know. 我认为这是不言而喻的,如果没有让我知道。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM