Get content that is before the number using regex

2
$MinhaString = "Aventura, Fantasia, Ação 25 de maio a 31 de maio"

I tried to do so, but it does not work.

$Genero = strstr ($MinhaString,"/[^0-9]/",true);

I just need the "Adventure, Fantasy, Action". The date is not static.

    
asked by anonymous 26.05.2017 / 23:22

2 answers

2

You can do this using the preg_match method

preg_match('/^([^0-9]+).*/', "Aventura, Fantasia, Ação 25 de maio a 31 de maio", $matches);
print_r($matches);

The regex '/([^0-9]+).*/' will get all non-numerals before the first number. In your attempt you forgot to put the match in the regex for the rest of the string.

    
27.05.2017 / 00:18
2

This regex will work, it will capture all non-numeric content from the beginning of the line to the first occurrence of a digit.

^\D*(?=\d)

Explanation:

  • ^ indicates the beginning of the line.
  • \D* causes regex to capture all non-numeric characters of greedy form.
  • (?=\d) this is a positive lookahead with digit, it indicates that it should only capture \D* if there is a digit after the capture group and in that case already causes regex to stop the capture.

Replace your RegEx with this one and it will work.

Here's a test

    
27.05.2017 / 01:14