phpregexstring

Regular Expression for a specific ID in a bracket


I have little confidence when it comes to regular expressions. Writing this in PHP code.

I need to be able to filter out strings that follow this format, where the numbers can be 4~6 digits (numeric only):

$input = "This is my string with a weird ID added cause I'm a weirdo! (id:11223)";

I could simply remove the last word by finding the last position of a space via strrpos(); (it appears none of them have a trailing space from JSON feed), then use substr(); to cut it. But I think the more elegant way would be a substring. The intended output would be:

$output = trim(preg_replace('[regex]', $input));
// $output = "This is my string with a weird ID added cause I'm a weirdo!"

So this regex should match with the brackets, and the id: portion, and any contiguous numbers, such as:

(id:33585)
(id:1282)
(id:9845672)

Intending to use the preg_replace() function to remove these from a data feed. Don't ask me why they decided to include an ID in the description string... It blows my mind too why it's not a separate column in the JSON feed altogether.


Solution

  • Try using the pattern \(id:\d+\):

    $input = "Text goes here (id:11223) and also here (id:33585) blah blah";
    echo $input . "\n";
    $output = preg_replace("/\(id:\d+\)/", "", $input);
    echo $output;
    

    This prints:

    Text goes here (id:11223) and also here (id:33585) blah blah
    Text goes here  and also here  blah blah
    

    There is an edge case here, which you can see in the possible (unwanted) extract whitespace left behind after the replacement. We could try to get sophisticated and remove that too, but you should state what you expected output is.