# [SOLVED] (php) help with preg_match for empty/blank lines



## kyris (Oct 15, 2007)

hello. i am using file_get_contents(url) to get the source code of a webpage. i want to search within that code to find some text.

somewhere in the source code i have the following (example) of text:


```
<div>

        texttexttexttexttexttexttexttexttext<br />
texttexttexttexttext<br />
         texttexttexttexttext
</div>
```
exactly as it can be seen in the source code file. how can i get just the text using preg_match? i've used something like this:


```
$regex = '/<div> (.*?) <\/div>/';
preg_match($regex,$sourceCode,$result);
```
with no luck and I believe the problem is the empty lines in the source code. 

Any help is appreciated.

Thanks


----------



## reventon (Oct 16, 2009)

*Re: (php) help with preg_match for empty/blank lines*

Hi,

Please take note of the following:


> s (PCRE_DOTALL)
> If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. *Without it, newlines are excluded.* This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.


From - PHP: Possible modifiers in regex patterns - Manual

So, change the modifier so dot metacharacters match newlines too and the expression should work.


----------



## kyris (Oct 15, 2007)

*Re: (php) help with preg_match for empty/blank lines*

hi reventon and thanks for your reply. so you mean i should change $regex to:


```
$regex = '/<div> (.*?) <\/div>/s';
```
(just add the 's' at the end)? I have tried this with no luck. Again $result is empty..


----------



## Laxer (Dec 28, 2010)

*Re: (php) help with preg_match for empty/blank lines*

I suppose I can try....

Change your regex line to this:


```
$regex= '/<div[^>]*>(.*?)<\/div>/i';
```
Please note, the above *should* work even if the starting div has a class/ID on it.

Example:


```
<div class="bold">text</div>
```
It would still get the text inside of the tag.


----------



## kyris (Oct 15, 2007)

*Re: (php) help with preg_match for empty/blank lines*

Hi Laxer. Thanks for the reply. The regex you propose does not work when there are blank lines in the string. eg:


```
<div>
text</div>
```


----------



## kyris (Oct 15, 2007)

*Re: (php) help with preg_match for empty/blank lines*

Actually I managed to solve it. I guess the /s modifier finally did the job. I just had to remove the space between the 2 tags.

This is the correct solution therefore, if you're interested:


```
$regex = '/<div>(.*?)<\/div>/s';
$test = "<div>

        texttexttexttexttexttexttexttexttext<br />
   texttexttexttexttext<br />

         texttexttexttexttext
		 
</div>";
preg_match($regex,$test,$postContent);
```
Thank you both for your help!

Kyris


----------

