0

My string looks like below:

Response: 311768560
311768562
311768564

I have tried:

$success_pattern="Response: ((\d+)[\n])*"; 
$response="Response: 311768560\n311768562\n311768564\n311768566";
preg_match("/$success_pattern/i", $response, $match);

Output:

Array ( 
    [0] => Response: 311768560 311768562 311768564
    [1] => 311768564
    [2] => 311768564
)

I need an array containing all the numbers as output like:

array('311768560','311768562','311768564');
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
mateors
  • 11

3 Answers3

0
<?php

$string="Response: 311768560 311768562 311768564";
preg_match_all("/[\d]+/", $string, $matches);
print_r($matches);

Output:

Array
(
    [0] => Array
        (
            [0] => 311768560
            [1] => 311768562
            [2] => 311768564
        )

)
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • Thank you so much for your answer, but i want use only preg_match not preg_match_all – mateors Mar 23 '17 at 06:24
  • @mateors For finding multiple matches you have to go for `preg_match_all` – Sahil Gulati Mar 23 '17 at 06:29
  • i refer to this site https://regex101.com/ they said A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data, so i guess there should be a solution, are you sure preg_match_all is the only solution? thanks in advance for your help. – mateors Mar 23 '17 at 06:31
  • http://stackoverflow.com/questions/3578671/unknown-modifier-g-in-when-using-preg-match-in-php – Sahil Gulati Mar 23 '17 at 06:35
0

Try not to get too hung up on a specific function like preg_match(). PHP offers several ways to generate your desired output. I'll show you three different approaches:

Input:

$response = "Response: 311768560
311768562
311768564";

Method #1: explode() with substr() and strpos() (Demo)

$array = explode("\r\n", substr($response, strpos($response, ' ') + 1));

*note, this method assumes that the first string (Response ) is the only unwanted substring and that it is always first. Also, the \r may not be necessary depending on your coding environment. This is probably the fastest of my list of methods because it doesn't use regex, but it does require 3 function calls and one incrementation -- not to mention it may be too literal for your actual use case.

Method #2: preg_match_all() (Demo)

$array = preg_match_all('/\d+/', $response, $out) ? $out[0] : [];

This method is very direct, fast, and requires minimal code. If there are any drawbacks at all, they are that the preg_match_all() returns a true|false result and generates an output variable in the form of a multidimensional array. To modify this output to suit your one-dimensional requirement, I place an inline condition at the end of the function which delivers the desired data to $array.

Method #3: preg_split() (Demo)

$array = preg_split('/\D+/', $response, 0, PREG_SPLIT_NO_EMPTY);

This function behaves just like explode() except it wields the power of regex. The pattern identifies all non-digital substrings and uses them as "delimiters" and splits the string on each of them. For your input, the first delimiter is "Response: ", then the newline character(s) after: "311768560" and "311768562". The beauty of preg_split() is that it directly provides the one-dimensional array that you are seeking.

Output:

No matter which of the above methods you try, you will receive the same correct output: $array = array('311768560', '311768562', '311768564');

If any of these methods fail your actual use case, then it is probably the product of your $response string being too different from the sample data that you have posted here.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
-1

Use Array_shift() function

<?php
$str = "Response: 311768560 311768562 311768564";
$s = explode(" ",$str);
$p = array_shift($s);
print_r($s);
?>

output

Array (
    [0] => 311768560
    [1] => 311768562
    [2] => 311768564 )
Siddhartha esunuri
  • 1,104
  • 1
  • 17
  • 29