Skip to content
Advertisement

Why preg_match() returns first match at index 1 (not 0) of returned array?

Im working on small website scraper with cURL.

I decided to use preg_match to find header and article content.

This is my code:

preg_match('@<h2 class="title">(.*?)</h2>@s', $this->website, $this->title);
            
if(sizeof($this->title) > 1)
    $this->title = trim($this->title[1]); // rewrite first element of array to regular variable
    

I was experimenting with it and I found, that if there is one match – it returns it in array at index 1, not 0.

Edited question: Why is this 1, not 0? Im doing something wrong?

My server: Apache/2.4.3 (Win32) PHP/5.4.7

Advertisement

Answer

The default behaviour of preg_match is to return the entire string which was matched in the result array at index 0, then each matched sub-pattern in subsequent result array indexes. If nothing was matched, the result array is empty. If something is matched, you get the full string that was matched and then any sub-patterns.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement