Juan Bonifacio
Juan Bonifacio

Reputation: 1

Regular Expression - Extract Data string php long in array

can you help me, have a string

(1, '525222313', 1, 'gfdsgsdfgdfsgsdfgfsdg', NULL, NULL),
(2, '789492261', 1, 'cbxbcvwewwwwwww', NULL, NULL),
(3, '1011587562', 1, 'cbcvnnjjjjjjjjjj', NULL, NULL),

I want extract in array PHP...

$data = [525222313,789492261,1011587562];

Upvotes: 0

Views: 63

Answers (3)

Juan Bonifacio
Juan Bonifacio

Reputation: 1

well, my string this is...

     $data = "CREATETABLEIFNOTEXISTS`blocked3`(`id`int(11)NOTNULLAUTO_INCREMENT,`fb_id`text,`status`int(11)DEFAULTNULL,`original`text,`created_at`datetimeDEFAULTNULL,`updated_at`datetimeDEFAULTNULL,PRIMARYKEY(`id`))ENGINE=MyISAMDEFAULTCHARSET=latin1AUTO_INCREMENT=900;INSERTINTO`blocked3`(`id`,`fb_id`,`status`,`original`,`created_at`,`updated_at`)VALUES(1,'525222313',1,'canceladosdeianmmanuel',NULL,NULL),(2,'789492261',1,'canceladosdeianmmanuel',NULL,NULL),(3,'1011587562',1,'canceladosdeianmmanuel',NULL,NULL),(4,'1017826711',1,'canceladosdeianmmanuel',NULL,NULL),(5,'1055942382',1,'canceladosdeianmmanuel',NULL,NULL),(6,'1082213165',1,'canceladosdeianmmanuel',NULL,NULL),(7,'1109407283',1,'canceladosdeianmmanuel',NULL,NULL),(8,'1137834449',1,'canceladosdeianmmanuel',NULL,NULL),(9,'1142349901',1,'canceladosdeianmmanuel',NULL,NULL),(899,'100028872278068',1,'canceladosdeianmmanuel',NULL,NULL);";

function ...

            $rows = preg_split('@,\R@', $data);
            foreach($rows as $row) {
            $row   = trim($row, '()');
            $line  = str_getcsv($row, ",", "'");
            $out[] = $line[1];
            }

this result....

   Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => `fb_id` [7] => 789492261 
   [8] => 1011587562 [9] => 1017826711 [10] => 1055942382 [11] => 1082213165 [12] => 
   1109407283 [13] => 1137834449 [14] => 1142349901 [15] => 100028872278068 )

The first value "525222313" is not found :(

Upvotes: 0

Progrock
Progrock

Reputation: 7485

<?php
$str =<<<STR
(1, '525222313', 1, 'gfdsgsdfgdfsgsdfgfsdg', NULL, NULL),
(2, '789492261', 1, 'cbxbcvwewwwwwww', NULL, NULL),
(3, '1011587562', 1, 'cbcvnnjjjjjjjjjj', NULL, NULL),
STR;

if(preg_match_all('/\d{2,}/', $str, $matches))
    print_r($matches[0]);

Output:

Array
(
    [0] => 525222313
    [1] => 789492261
    [2] => 1011587562
)

Upvotes: 1

Progrock
Progrock

Reputation: 7485

<?php

$str =<<<STR
(1, '525222313', 1, 'gfdsgsdfgdfsgsdfgfsdg', NULL, NULL),
(2, '789492261', 1, 'cbxbcvwewwwwwww', NULL, NULL),
(3, '1011587562', 1, 'cbcvnnjjjjjjjjjj', NULL, NULL),
STR;

$rows = preg_split('@,\R@', $str);
foreach($rows as $row) {
    $row   = trim($row, '()');
    $line  = str_getcsv($row, ",", "'");
    $out[] = $line[1];
}

print_r($out);

Output:

Array
(
    [0] => 525222313
    [1] => 789492261
    [2] => 1011587562
)

Upvotes: 0

Related Questions