Link to home
Create AccountLog in
Avatar of Jerry_Pang
Jerry_Pang

asked on

Need help, find BODY tag and values (Pattern matching)

$URL_GRAB = "<body onclick='asdsd asd> ' asd> asd </body>";

<textarea name="result" rows="30" cols="80">
<?php
if (preg_match_all("/^<body.*?|>.*?<\/body>$/i", $URL_GRAB, $matches))
{
print_r($matches);
}
else
{
print_r('none');
}
}
?>
</textarea>


closes *result* i could get is

Array
(
    [0] => Array
        (
            [0] => <body
            [1] => > \' asd> asd </body>
        )

)


How do i make it
Array
(
    [0] => Array
        (
            [0] => <body onclick='asdsd asd> ' asd>
            [1] => asd
            [2] => </body>
        )

)

i am having trouble getting my pattern matching right
this pattern "/^<body.*?|>.*?<\/body>$/i" is lacking..


i want a pattern that
starts with
(
      starts with <body
      zero or more
      (zero or more [a-zA-Z]characters
            (' and matching ending tag ' | " and matching ending tag " )
      )
      ends with >
)
followed by any characters
ends with </body>

help.. going nuts with pattern matching. i tried different pattern matching but it doesnt work, this is the closiest i could get.
SOLUTION
Avatar of pvginkel
pvginkel
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of Jerry_Pang
Jerry_Pang

ASKER

hi.. what does the following mean?
[^>]
/m
/s

the most important element that i need is only the  [1] => asd
only what is inside the <body> tag.

i just realize its kinda like xml. but i only need whats inside it.
i tried it on real example like the following..

pvginkel, Roonaan  the array [1] is correct. i was wondering why there is no <\/body> in any results.
Array
(
    [0] => <body bgcolor=\"#FFFFFF\" leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\" vlink=\"#666666\" text=\"#000000\">
<basefont size=\"3\">
<!-- BEGIN NO PRINT -->
</body>
    [1] =>
<basefont size=\"3\">
<!-- BEGIN NO PRINT -->

)



thyminfo, it does now work on the above example. but i added the Roonaan's #/ims, and it works fine.
"/^(<body[^<]*>)(.*)(<\/body>)$/ims"