Search code examples
phpregexpreg-matchpreg-match-all

Regex guide to remove digits from string in special conditions only


I am in need to remove digits from a string with PHP code. But here is the catch —

  1. If digits are in the string and have space after and before them
  2. If they are at the start of the string then it needs space after the digit.
  3. If they are at the end of the string then it needs space before the digit.

Ex "need 4 speed" shall become "need speed"

"4 speed" shall become "speed"

"speed 4" shall become "speed"

At the same time

"4speed" shall remain as it is

"speed4" shall remain as it is

"speed2car" shall remain as it is

Currently using below code which removes all digits eventually which is not correct for a given logic

$clean_string = preg_replace('/\d/', '', $dirty_string );

Any help here will be appreciated.


Solution

  • You can match the variants at the start and the end of the string, and capture the ones that have spaces before and after the digits.

    Using preg_replace_callback, replace the matches from the start and the end with an empty string, and replace what is captured that contains spaces before and after with a single space.

    ^\d+\h+|\h+\d+$|(\h+\d+\h+)
    

    Explanation

    • ^\d+\h+ Start of string, match 1+ digits and 1+ horizontal whitespaces
    • | Or
    • \h+\d+$ Match 1+ horizontal whitespaces, 1+ digits at the end of the string
    • | Or
    • (\h+\d+\h+) Capture group 1, match 1+ digits between 1+ horizontal whitepaces

    Regex demo | Php demo

    $lines = [
        "need 4 speed",
        "4 speed",
        "speed 4",
        "4speed",
        "speed4",
        "speed2car"
    ];
    
    foreach ($lines as $line) {
        $pattern = "~^\d+\h+|\h+\d+$|(\h+\d+\h+)~";
        echo preg_replace_callback($pattern, function($matches){
            return array_key_exists(1, $matches) ? " " : "";
        }, $line) . PHP_EOL;
    }
    

    Output

    need speed
    speed
    speed
    4speed
    speed4
    speed2car