[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

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.
0
Jerry_Pang
Asked:
Jerry_Pang
3 Solutions
 
pvginkelCommented:
I'm not really clear what you want, but this will make the regexp working:

/<body.*>(.*)<\/body>/isU
0
 
RoonaanCommented:
if (preg_match_all("#<body[^>]*>(.*)</body>#/ims", $URL_GRAB, $matches))

-r-
0
 
thyminfoCommented:
Hi!
If your ' $URL_GRAB' value is typed in correctly and if you are looking for result to be
Array
(
    [0] => Array
        (
            [0] => <body onclick='asdsd asd> ' asd>
            [1] => asd
            [2] => </body>
        )

)

you should use the following:

if (preg_match_all("/^(<body[^<]*>)(.*)(</body>)$/i", $URL_GRAB, $matches))

-Cheers!
0
 
Jerry_PangAuthor Commented:
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.
0
 
Jerry_PangAuthor Commented:
"/^(<body[^<]*>)(.*)(<\/body>)$/ims"
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now