Search code examples
phpregexpreg-match

Regular Expression with php Error


i have the following html structure :

<div data-provincia="mi" data-nazione="it" ecc...

I'm trying to take "mi" with preg_match function. This is my code :

$pattern = '/data-provincia=*"[a-zA-Z]*"/';
preg_match($pattern,$element,$provincia);

I think that the code is right but it doesn't match with anything. Where i'm wrong? Thanks.


Solution

  • You might want to use the quantifier + (1 or more) next to the character class between brackets, and remove the first star. Also added a subtpattern for you to get exactly the part you want. Give it a try :

    $pattern = '/data-provincia="([a-zA-Z]+)"/';
    preg_match($pattern,$element,$provincia);
    echo $provincia[1];