Syed ObaidUllah Naqvi
Syed ObaidUllah Naqvi

Reputation: 474

Php regex multiline search issue

I am trying to get all characters between style tag. this is my regex

 '#<style>(.*?)</style>'

It is producing result correctly However it is not working for multi line

Working correctly for this :

<style>body { height: '100%'; }</style>

This is not working

<style> 
body { 
  height: '100%'; 
}

</style>

I know /s or [/s/S] or s would work but I dont how they will work

Regards

Upvotes: 0

Views: 63

Answers (3)

perilla
perilla

Reputation: 38

"." matches any single character except \n,so '<style>(.*?)</style>' wouldn't work in multi line situation. while [\s\S]* will match any character including \n.

Upvotes: 0

Mat
Mat

Reputation: 2154

You can use pattern modifiers to match on multilines with PCRE regex : http://php.net/manual/en/reference.pcre.pattern.modifiers.php

m is the multiline modifier. s is the modifier to allow dot to match new lines too. So how about a pattern like this one:

'#<style>(.*?)</style>#ms'

I think you can also be interested in the imodifier since tags can be worded <style> or <STYLE> or other flavors <StYlE> wich are all valid.

Upvotes: 1

Pruthvi Raj
Pruthvi Raj

Reputation: 3036

use this:

 <style>([\s\S]*)<\/style>

DEMO

Upvotes: 0

Related Questions