在php中通过空格读取字符串

Posted

技术标签:

【中文标题】在php中通过空格读取字符串【英文标题】:read string by white spaces in php 【发布时间】:2021-09-09 11:08:17 【问题描述】:

我试图在laravel 5.6 中使用此库\Smalot\PdfParser\Parser(); 阅读PDF

我的所有内容都正常,但我有这个:

Array
(
    [0] =>  MARTIN CARRILLO MARIA ESMERALDA ALHAMBRA 10 958 54 38 93
    [1] =>  ESPIGARES DIAS JOSE ANTONIO ALHAMBRA 11 958 54 33 32
    [2] =>  GUTIERREZ TITOS JOSE MANUEL ALHAMBRA 12 958 54 04 10
    [3] =>  MARTIN COBOS ANTONIO ALHAMBRA 18 958 54 33 28
    [4] =>  GOMEZ CARRILLO JOSE ALHAMBRA 20 958 54 32 72
    [5] =>  RODRIGUEZ RUANO BUENAVENTURA ALHAMBRA 21 958 54 35 86
    [6] =>  GARCIA ARIAS MARIA ISABEL ALHAMBRA 22 958 54 07 87
    [7] =>  RODRIGUEZ JIMENEZ MIGUEL ALHAMBRA 27 958 54 08 77
    [8] =>  GUTIERREZ FERNANDEZ AMANDA HILDA ALHAMBRA 3 958 49 98 62
    [9] =>  DIAZ FLORIAN DOLORES ALHAMBRA 30 958 54 33 99
    [10] =>  PEREZ LASTRA SAUL ADAN ALHAMBRA 32 958 54 31 46
    [11] =>  AMIGO MOLINA MARIA MATILDE ALHAMBRA 35 958 54 31 91
    [12] =>  ESPIGARES GOMEZ JORGE ALHAMBRA 37 958 54 02 22
    [13] =>  ESPIGARES ESPIGARES JOSE ALHAMBRA 40 958 54 31 83

但我需要从这些字符串中获取第二个姓氏、姓氏、姓名、地址和电话...我认为我可以获取所有数据计数空格。

我正在尝试这个:

// loop to get data line
foreach($dataByLine as &$line)      
    // delete first element in string
    $cleanString = substr($line, 0, 7);
    $line = str_replace($cleanString, "", $line);   
    
    $line = preg_replace('/^[\d]+/','',$line);
    //$line = ltrim($line,"0123456789");
    $stringDivide = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);

    
    //array_push($secondSurnameArray, $stringDivide[0]);

    array_push($dataList, $line);


print_r($dataList);

但是这个返回:

Array
(
    [0] => MARTIN
    [1] => CARRILLO
    [2] => MARIA
    [3] => ESMERALDA
    [4] => ALHAMBRA
    [5] => 10
    [6] => 958
    [7] => 54
    [8] => 38
    [9] => 93
)
Array
(
    [0] => ESPIGARES
    [1] => DIAS
    [2] => JOSE
    [3] => ANTONIO
    [4] => ALHAMBRA
    [5] => 11
    [6] => 958
    [7] => 54
    [8] => 33
    [9] => 32
)
Array
(
    [0] => GUTIERREZ
    [1] => TITOS
    [2] => JOSE
    [3] => MANUEL
    [4] => ALHAMBRA
    [5] => 12
    [6] => 958
    [7] => 54
    [8] => 04
    [9] => 10
)

例如,但如果我这样做:

array_push($secondSurnameArray, $stringDivide[0]);

这将返回未定义的索引 0。

我需要这些数据来创建一个数组来创建插入查询。

感谢您的帮助,阅读我,对不起我的英语。

更新

我希望这样:

$secondSurname = ['MARTIN', 'ESPIGARES', 'GUTIERREZ'....etc]
$surname = ['CARRILLO', 'DIAS', 'TITOS', .....etc]
$name = ['JOSE', 'JOSE', 'ANTONIO']
$addres = ['ALHAMBRA 10', 'ALHAMBRA 11', 'ALHAMBRA 12', etc]
$phone = ['958 54 38 93', '958 54 33 32', '958 54 04 10']

在我需要与$secondSurname, $surname and $name 进行数组合并后进行插入查询

【问题讨论】:

你想要什么样的预期输出?展示您预期结果的样本。 @AnantKumarSingh 感谢您的回复。我用 mi Hope return 更新了我的问题 不可能,因为没有固定模式,例如哪些数据将转到 name、surnmae、secondSurname。就像在您想要的示例输出中一样,对于您缺少的第一个用户 MARIA ESMERALDA ?为什么这两个被跳过了?第二个用户ANTONIO 丢失 创建 pdf 时不要使用空间,而是使用- 分隔值,这样当您阅读它时,很容易根据- 拆分 sting 并直接获取数据而没有任何问题。例如:MARTIN-CARRILLO MARIA ESMERALDA-ALHAMBRA 10-958 54 38 93 @AnantKumarSigh 感谢您的回复。例如,我还需要 Maria、Esmeralda... 等等我忘了等等 【参考方案1】:

如果有 2 个姓氏,我假设您正在尝试释放额外的姓氏,以便在循环中轻松完成。

也可以在这里简单地合并构成电话号码的部分。

$dataByLine = [ 
    'MARTIN CARRILLO MARIA ESMERALDA ALHAMBRA 10 958 54 38 93',
    'ESPIGARES DIAS JOSE ANTONIO ALHAMBRA 11 958 54 33 32',
    'GUTIERREZ TITOS JOSE MANUEL ALHAMBRA 12 958 54 04 10',
    'MARTIN COBOS ANTONIO ALHAMBRA 18 958 54 33 28',
    'GOMEZ CARRILLO JOSE ALHAMBRA 20 958 54 32 72',
    'RODRIGUEZ RUANO BUENAVENTURA ALHAMBRA 21 958 54 35 86',
    'GARCIA ARIAS MARIA ISABEL ALHAMBRA 22 958 54 07 87',
    'RODRIGUEZ JIMENEZ MIGUEL ALHAMBRA 27 958 54 08 77',
    'GUTIERREZ FERNANDEZ AMANDA HILDA ALHAMBRA 3 958 49 98 62',
    'DIAZ FLORIAN DOLORES ALHAMBRA 30 958 54 33 99',
    'PEREZ LASTRA SAUL ADAN ALHAMBRA 32 958 54 31 46',
    'AMIGO MOLINA MARIA MATILDE ALHAMBRA 35 958 54 31 91',
    'ESPIGARES GOMEZ JORGE ALHAMBRA 37 958 54 02 22',
    'ESPIGARES ESPIGARES JOSE ALHAMBRA 40 958 54 31 83'
];

$secondSurname  = [];
$surname        = [];
$name           = [];
$address        = [];
$phone          = [];

foreach($dataByLine as $line)      
    $t = explode(' ', $line);   # make array from string using space as seperator
    // fix double surname issue
    if ( count($t) == 10 )
        // we have a line with a double surname
        array_shift($t);   # drop the first name in list
    
    $secondSurname[]    = $t[0];
    $surname[]          = $t[1];
    $name[]             = $t[2];
    $address[]          = $t[3];
    $phone[]            = $t[4] . $t[5] . $t[6] . $t[7] . $t[8]; 
    # you can add space and or `-` into the phone if you like


echo 'SecondSurnames ';
print_r($secondSurname);
echo 'Surnames ';
print_r($surname);
echo 'names ';
print_r($name);
echo 'address ';
print_r($address);
echo 'phone ';
print_r($phone);

结果

SecondSurnames Array
(
    [0] => CARRILLO
    [1] => DIAS
    [2] => TITOS
    [3] => MARTIN
    [4] => GOMEZ
    [5] => RODRIGUEZ
    [6] => ARIAS
    [7] => RODRIGUEZ
    [8] => FERNANDEZ
    [9] => DIAZ
    [10] => LASTRA
    [11] => MOLINA
    [12] => ESPIGARES
    [13] => ESPIGARES
)
Surnames Array
(
    [0] => MARIA
    [1] => JOSE
    [2] => JOSE
    [3] => COBOS
    [4] => CARRILLO
    [5] => RUANO
    [6] => MARIA
    [7] => JIMENEZ
    [8] => AMANDA
    [9] => FLORIAN
    [10] => SAUL
    [11] => MARIA
    [12] => GOMEZ
    [13] => ESPIGARES
)
names Array
(
    [0] => ESMERALDA
    [1] => ANTONIO
    [2] => MANUEL
    [3] => ANTONIO
    [4] => JOSE
    [5] => BUENAVENTURA
    [6] => ISABEL
    [7] => MIGUEL
    [8] => HILDA
    [9] => DOLORES
    [10] => ADAN
    [11] => MATILDE
    [12] => JORGE
    [13] => JOSE
)
address Array
(
    [0] => ALHAMBRA
    [1] => ALHAMBRA
    [2] => ALHAMBRA
    [3] => ALHAMBRA
    [4] => ALHAMBRA
    [5] => ALHAMBRA
    [6] => ALHAMBRA
    [7] => ALHAMBRA
    [8] => ALHAMBRA
    [9] => ALHAMBRA
    [10] => ALHAMBRA
    [11] => ALHAMBRA
    [12] => ALHAMBRA
    [13] => ALHAMBRA
)
phone Array
(
    [0] => 10958543893
    [1] => 11958543332
    [2] => 12958540410
    [3] => 18958543328
    [4] => 20958543272
    [5] => 21958543586
    [6] => 22958540787
    [7] => 27958540877
    [8] => 3958499862
    [9] => 30958543399
    [10] => 32958543146
    [11] => 35958543191
    [12] => 37958540222
    [13] => 40958543183
)

注意:我不确定这些单独的数组是否真的是传递给查询绑定的最佳解决方案,这就是我要求查看查询的原因。 为每个人创建一个数组可能更有意义。

【讨论】:

好的,我想最后一次修复 @scorpions78 在您提出的解决方案中您说'ALHAMBRA 10,ALHAMBRA 12...etc。现在,当前的解决方案非常适合您。近期请注意不要误导人,给他们准确的细节,否则您将无法得到正确的解决方案

以上是关于在php中通过空格读取字符串的主要内容,如果未能解决你的问题,请参考以下文章

如何在PHP中通过分隔符将字符串分解为多维数组

在Firefox中通过PHP或CSS用省略号截断长字符串[重复]

如何在Mysql查询中通过PHP对加密数据进行排序

php中通过Hashids将整数转化为唯一字符串

如何在php中通过引用传递无限参数[重复]

如何在 C++ 中读取由空格分隔的字符串? [关闭]