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

混江龙づ霸主 提交于 2021-01-28 19:41:57

问题


Im working on small website scrapper 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


回答1:


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.



来源:https://stackoverflow.com/questions/14383484/why-preg-match-returns-first-match-at-index-1-not-0-of-returned-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!