Search code examples
phpregexstringpreg-matchbetween

php regex string between


I have this string:

<span itemprop="author" itemscope="" itemtype="http://schema.org/Person"><span itemprop="name">gegi123</span></span>

and i need gegi123

I tried this:

/<span itemprop="author" itemscope="" itemtype="http://schema.org/Person"><span itemprop="name">(.*?)</span></span>/

but that shows an error:

Warning: preg_match_all(): Unknown modifier '/' in /var/www/post/main.php on line 29

Solution

  • You need to escape the pattern delimiter to match it literally by a backslash or pick another delimiter.

    Unknown modifier '/' ...

    Everything after the closing delimiter is considered modifiers/flags:

    /<span itemprop="author" itemscope="" itemtype="http://schema.org/Person"...
    ↑ opening delimiter                                  ↑ closing delimiter
                                                          ↑ php don't know modifier /
    

    With another delimiter: ~<span...http://... ~
     By escaping delimiter: /<span...http:\/\/... /