Remove string numeric sequence

Asked

Viewed 105 times

3

I have the following problem, I believe I can solve it with regular expressions, but I am terrible with the same.

Let’s say I have a string that way: CONECTOR BNC FEMEA + F MACHO 024267 or so PLUG P-4 UNIVERSAL 006-096, the position of the numbers can be anywhere, beginning, middle or end, need to take out the numerical sequences that are equal to or greater than 3 digits.

These digits are usually as a reference and I am using this information to search for images using the Google Custom Search API, if they are passed to perform the search usually does not return any results.

The desired result is: Entree: CONECTOR BNC FEMEA + F MACHO 024267 Exit: CONECTOR BNC FEMEA + F MACHO

1 answer

4


To capture/replace three or more digits in a row use regex \d{3,}. \d means numbers (0-9), keys act as a quantifier in case they require three digits or more to be captured.

$str = 'CONECTOR BNC FEMEA + F MACHO 024267 23';
$nova = preg_replace('/\d{3,}/', '', $str);
echo $nova;

Upshot:

CONECTOR BNC FEMEA + F MACHO 23
  • It worked perfectly, thank you very much. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.