0

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];
4
  • What's the problem? It looks like it should be easy to do with preg_match_all(). Commented Jan 11, 2019 at 20:49
  • If you want to use a non-RegularExpression way, you could try this sandbox.onlinephpfunctions.com/code/… (Not posting an answer because it's not a regex response) Commented Jan 11, 2019 at 21:23
  • Are you trying to parse a mysql dump with PHP? Commented Jan 11, 2019 at 23:36
  • Try $re = '/\(\d+,\h*\'(\d+)\'/'; see demo Commented Jan 12, 2019 at 12:04

3 Answers 3

1
<?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
)
Sign up to request clarification or add additional context in comments.

2 Comments

This will match any 2 or more digits. Which could pluck out unwanted items.
We could match against the single quotes too: if(preg_match_all("/'(\d{2,})'/", $str, $matches)) print_r($matches[1]);
0
<?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
)

Comments

0

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 :(

1 Comment

Your input is different to your original question. Try splitting it on VALUES, and either adapting the trim above (add a semi-colon to your trimmiable chars) or using the preg_match_all solution. If you go with the latter, pick the pattern in the comments, as otherwise you might match an another column/value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.