-->

PHP Extract numbers from a string

2020-05-03 08:59发布

问题:

I want to extract numbers from a string in PHP like following :

if the string = 'make1to6' i would like to extract the numeric character before and after the 'to' substring in the entire string. i.e. 1 and 6 are to be extracted

i will be using these returned values for some calculations.' i would like to extract the numeric character before and after the 'to' substring in the entire string. i.e. 1 and 6 are to be extracted

The length of the string is not fixed and can be a max of 10 characters in length.The number can be of max two digits on either side of 'to' in the string.

Some example string values :

sure1to3
ic3to9ltd
anna1to6
joy1to4val
make6to12
ext12to36

thinking of something like :

function beforeTo(string) {

    return numeric_value_before_'to'_in_the_string;

}


function afterTo(string) {

    return numeric_value_after_'to'_in_the_string;

}

i will be using these returned values for some calculations.

回答1:

You can use a regular expression as such, it should match exactly your specification:

$string = 'make6to12';
preg_match('{^.*?(?P<before>\d{1,2})to(?P<after>\d{1,2})}m', $string, $match);
echo $match['before'].', '.$match['after']; // 6, 12


回答2:

You could use preg_match_all to achive this:

function getNumbersFromString($str) {
    $matches = array();
    preg_match_all("/([0-9]+)/",$str,$matches);
    return $matches;
}
$matches = getNumbersFromString("hej 12jippi77");


回答3:

You can use this:

// $str holds the string in question
if (preg_match('/(\d+)to(\d+)/', $str, $matches)) {
    $number1 = $matches[1];
    $number2 = $matches[2];
}


回答4:

You can use regular expressions.

$string = 'make1to6';
if (preg_match('/(\d{1,10})to(\d{1,10})/', $string, $matches)) {
    $number1 = (int) $matches[1];
    $number2 = (int) $matches[2];
} else {
    // Not found...
}


回答5:

<?php

$data = <<<EOF

sure1to3
ic3to9ltd
anna1to6
joy1to4val
make6to12
ext12to36

EOF;

preg_match_all('@(\d+)to(\d+)@s', $data, $matches);
header('Content-Type: text/plain');

//print_r($matches);
foreach($matches as $match)
{
    echo sprintf("%d, %d\n", $match[1], $match[2]);
}

?>


回答6:

This is what Regular Expressions are for - you can match multiple instances of very specific patterns and have them returned to you in an array. It's pretty awesome, truth be told :)

Take a look here for how to use the built in regular expression methods in php : LINK

And here is a fantastic tool for testing regular expressions: LINK



回答7:

<?php
list($before, $after) = explode('to', 'sure1to3');

$before_to = extract_ints($before);
$after_to  = extract_ints($after);

function extract_ints($string) {
    $ints = array();
    $len = strlen($string);

    for($i=0; $i < $len; $i++) {
        $char = $string{$i};
        if(is_numeric($char)) {
            $ints[] = intval($char);
        }
    }

    return $ints;
}
?>

A regex seems really unnecessary here since all you are doing is checking is_numeric() against a bunch of characters.



回答8:

Use preg_match with a regex that will extract the numbers for you. Something like this should do the trick for you:

$matches = null;
$returnValue = preg_match('/([\d+])to([\d+])/uis', 'ic3to9ltd', $matches);

After this $matches will look like:

array (
  0 => '3to9',
  1 => '3',
  2 => '9',
);

You should read somewhat on regular expressions, it's not hard to do stuff like this if you know how they work. Will make your life easier. ;-)