skip to Main Content

I’ve strings like constant-string-NUMBER-* where

  • constant-string- is a costant string (that I know and can use in the effort of getting the NUMBER) e.g. fix-str-
  • NUMBER is any natural number
  • -* can be any string

String-result examples:

fix-str-0
// result: 0

fix-str-0-another-str
// result: 0

fix-str-123
// result: 123

fix-str-456789
// result: 456789

fix-str-123456789-yet-another-str
// result: 1234567899

fix-str-999999-another-str-123
// result: 999999

I would like to extract the NUMBER from those strings in PHP so that I can associate this number to a variable e.g. $numberFromString = ?.

Any insight?

3

Answers


  1. You can represent a string as an array of characters. Use the PHP substr() Function, where the second argument is the number from which your string is left.

    Example. Return "world" from the string:

    <?php
    echo substr("Hello world",6);
    ?>
    

    Info from here: https://www.w3schools.com/php/func_string_substr.asp

    Login or Signup to reply.
  2. Try this:

    fix-str-(d+)
    
    • fix-str- match this string.
    • (d+) followed by one or more digits, and save the number inside the first capturing group.

    EDIT

    From @user3783243, we can also use fix-str-Kd+, without the need of a capturing group.

    fix-str-Kd+
    
    • fix-str- match this string, then..
    • K reset the starting point of the reported match.
    • d+ then match one or more digits.

    See regex demo

    <?php 
    $str ="fix-str-123456789-yet-another-str fix-str-234";
    
    $pattern = "/fix-str-Kd+/";
    
    preg_match($pattern, $str, $arr1); //Find only the first match.
    
    echo "The first match: " . $arr1[0]; //Output: 123456789
    
    echo "nnn";
    
    preg_match_all($pattern, $str, $arr2); //Find all the matches.
    
    echo "All the matches: " . implode(',', $arr2[0]); //Output: 123456789,234
    ?>
    
    
    Login or Signup to reply.
  3. Based on your string examples, there are two possible ways. One way could use explode, then of course the other way could be with preg_match for regex. I’ll show both ways, only to show that regex is not always absolutely necessary.

    Using explode:

    $strings = [
    'fix-str-0',
    'fix-str-0-another-str',
    'fix-str-123',
    'fix-str-456789',
    'fix-str-123456789-yet-another-str',
    'fix-str-999999-another-str-123',
    ];
    
    $match = [];
    $matches = [];
    foreach ($strings as $string) {
        $match = explode('-', $string);
        
        if (count($match) >= 3) {
            $matches[] = $match[2]; // Array offset 2 has the number
        }
    }
    
    foreach($matches as $found) {
        echo $found, PHP_EOL;
    }
    
    // Output:
    
    0
    0
    123
    456789
    123456789
    999999
    

    Using preg_match:

    $strings = [
    'fix-str-0',
    'fix-str-0-another-str',
    'fix-str-123',
    'fix-str-456789',
    'fix-str-123456789-yet-another-str',
    'fix-str-999999-another-str-123',
    ];
    
    $match = [];
    $matches = [];
    foreach ($strings as $string) {
          // match 1 or more digits, store to $match
        preg_match('/(d+)/', $string, $match);
        
        if (!empty($match)) {
            $matches[] = $match[0]; // use the first match
        }
    }
    
    foreach($matches as $found) {
        echo $found, PHP_EOL;
    }
    
    // Output:
    
    0
    0
    123
    456789
    123456789
    999999
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search