syntax for preg function in php

druagord's picture

He has: 335 posts

Joined: May 2003

hello i use this to split the head element from the body element when reading a html file

<?php
preg_match_all
(\"/(<head>.*<\/head>)|(<body.*<\/body>)/smi\",$file,$arrResult);
?>

the problem is that even with i at the end the the expression is case sensitive i found on php.net that you could correct that with (?i) in the expression but i don't find the correct syntax

i know this would work but i don't like it

<?php
preg_match_all
(\"/(<[hH][eE][aA][dD]>.*<\/[hH][eE][aA][dD]>)|(<[bB][oO][dD][yY].*<\/[bB][oO][dD][yY]>)/smi\",$file,$arrResult);
?>

IF , ELSE , WHILE isn't that what life is all about

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I'm having no case sensitivity problems with preg_match_all() with the "smi" modifiers.

http://host.maxalbert.com/testing_center/regex_exercise9.php

<?php
$string
=<<<myHTML
<html>
<head>
<title>this is the head</title>
</head>
<body>
this is the body
</body>
</html>
myHTML;

echo \
"<html><body>\";
echo \"<textarea rows=\\"
24\\" cols=\\"40\\" wrap=\\"off\\">$string</textarea>\";
echo \"&nbsp;<textarea rows=\\"
24\\" cols=\\"40\\" wrap=\\"off\\">\n\";

preg_match(\"!(<head>)(.*)(</head>)!smi\",
$string, $head);
preg_match(\"!(<body.*)(.*)(</body>)!smi\",
$string, $body);

print_r(
$head);
print_r(
$body);

echo \"</textarea></body></html>\";
?>

Mark Hensler
If there is no answer on Google, then there is no question.

druagord's picture

He has: 335 posts

Joined: May 2003

Did you try it with in $string instead the line i have works great if the tags are lower case that's why i don't think it's an error in the expression but in the modifier. I tried with "!" instead of "/" as delimiter but i get the same thing.

IF , ELSE , WHILE isn't that what life is all about

druagord's picture

He has: 335 posts

Joined: May 2003

Ok i now did like you with 2 preg_match and it works fine. i cant understand why preg_match_all wouldn't work anyway i'll use like this. Thanks for the help was really stuck on this one

IF , ELSE , WHILE isn't that what life is all about

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.