1

How can I adapt this function below to get string between numbers?

Function:

function getInbetweenStrings($start, $end, $str){
    $matches = array();
    $regex = "/$start([a-zA-Z0-9_]*)$end/";
    preg_match_all($regex, $str, $matches);
   return $matches[1];
}

Example:

1448459casa48912687
010697046a-parede-verde698012
456902sim-senhora4401254

Output expected:

casa
a-parede-verde
sim-senhora

Thank you. @edit: I don't want to remove all numbers, I need them! I just want grab string between.

3
  • Sorry, I don't want to remove all numbers, I need them! I just want grab string between. Commented May 23, 2019 at 8:18
  • What is supposed to happen in the case of something like 12345number10-downing-street54321 ? Commented May 23, 2019 at 8:20
  • @CD001 is possible to detect only first numbers and last numbers? The middle numbers in this situation belongs to the string. Commented May 23, 2019 at 8:21

3 Answers 3

2

You can use this regex, which looks for a minimal sequence (by adding ? to .*) of characters between starting and trailing digits:

^\d+(.*?)\d+$

In PHP:

$strings = array('1448459casa48912687',
                 '010697046a-parede-verde698012',
                 '456902sim-senhora4401254',
                 '12345number10-downing-street54321');

foreach ($strings as $string) {
    $between = preg_replace('/^\d+(.*?)\d+$/', '$1', $string);
    echo "$between\n";
}

Output:

casa
a-parede-verde 
sim-senhora
number10-downing-street

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

1

If the strings are different (ie: [foo] & [/foo])

function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}



$fullstring = '010697046a-parede-verde698012';
$parsed = get_string_between($fullstring, '010697046a', '698012');

echo $parsed; // (result = a-parede-verde)

3 Comments

Sorry, I don't want to remove all numbers, I need them! I just want grab string between.
The number will always change, I need a way to grab string between numbers that still always changing
then you're supposed to use preg_replace('/^\d+(.*?)\d+$/', '$1', $fullstring) which will grab the text between numbers i hope
0

The effect of Nick's answer is to remove all leading and trailing digits from the strings, but PHP already has trim() which can do this -- there is no need for the expense of a regex function.

Code: (Demo)

$array = [
    '1448459casa48912687',
    '010697046a-parede-verde698012',
    '456902sim-senhora4401254',
];

var_export(
    array_map(fn($v) => trim($v, '0..9'), $array)
);

Mind you, 0..9 is a bit of an odd syntax for PHP, but it is a mechanism to denote a range of characters on the ascii table. Read more about double-dot syntax.


If code brevity is more important than performance, you can affect the entire array with a single preg_replace() call -- just remove one or more digits which are at the start or end of the strings. (Demo)

var_export(
    preg_replace('/^\d+|\d+$/', '', $array)
);

Comments

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.