Getting onload="" from <body> tag

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Getting onload="" from <body> tag

Post by ben.artiss »

Hi everyone,

Sorry if this has been answered before (I have hunted on google and here but no joy). What I'm trying to do is check to see if the onload attribute has been set in the <body> tag, and take the contents of it if it is. So far I've got this snippet, but I don't know how to change it so that it captures the text between [^>] instead of between the body tags:

Code: Select all

preg_match_all('/<body[^>]*>(.*?)<\/body>/is', display::$template, $body);
Any help would be really appreciated!

Thanks in advance,
Ben
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Getting onload="" from <body> tag

Post by prometheuzz »

Try this:

Code: Select all

$s = 'foo <body onload="alert(\'Gotcha!\')"> ... </body> bar';
preg_match('/<body[^>]*?(?:onload="([^"]*)")?[^>]*>/i', $s, $match);
print_r($match);
/*
Array
(
    [0] => <body onload="alert('Gotcha!')">
    [1] => alert('Gotcha!')
)
*/
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Getting onload="" from <body> tag

Post by ben.artiss »

Hi prometheuzz thanks for the reply,

That's a lot closer than I ever would have got but it didn't quite do it, here's what I got:

Code: Select all

$s = 'foo <body onload="alert(\'Gotcha!\')"> ... </body> bar';
preg_match('/<body[^>]*?(?:onload="([^"]*)")?[^>]*>/is', $s, $match);
 
echo 'count = '.count($match).'<br />';
 
foreach ($match as $test) {
    echo htmlspecialchars($test).'<br />';
}
/*
Output:
 
count = 1
<body onload="alert('Gotcha!')">
*/
I suppose it would be ok to do another preg_match on that result but I know the preg functions are resource hungry so I'd really like to do it in one statement! :) Do you know how I could get just the onload="..." bit if it's available?

Thanks again for your help.

Regards,
Ben
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Getting onload="" from <body> tag

Post by ben.artiss »

I was a little lazy and settled for:

Code: Select all

preg_match('/onload="(.*?)"/i', display::$template, $match)
- but if you have the time to figure it out I'd be very interested to see how it's done properly! :)

Regards,
Ben
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Getting onload="" from <body> tag

Post by prometheuzz »

ben.artiss wrote:I was a little lazy and settled for:

Code: Select all

preg_match('/onload="(.*?)"/i', display::$template, $match)
- but if you have the time to figure it out I'd be very interested to see how it's done properly! :)

Regards,
Ben
Well, if it works for all your test cases, I guess that is the proper way then. I thought you also wanted to capture body-tags that did not have the onload-attribute, but I guess I'm not sure what exactly you're trying to do.
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Getting onload="" from <body> tag

Post by ben.artiss »

Hi again,

I have kind of settled and am not really happy with the way it works at the moment. What's happening is I have a template file for the appearance of each section (e.g. admin, moderator, public etc.), and the display class get's the content of the template and loads the appropriate content based on the URI (e.g. for http://localhost/ the template /template/public/index.php is loaded and /public/home/index.php is added to the template).

However, if someone were to add an onload to the body tag in the template, things would stop working because currently it simply replaces the <body> tag with <body onload="..."> (which is messy but I'm really no good at regexp!). So ideally, I would like to check the template to see if onload="..." is specified, and if it is, get what's currently between the "..." and add it to the static var display::$onload so I can add other onload events throughout the display class before outputting the page.

If that clears things up a bit I'd greatly appreciate a bit of know-how from someone who knows what they're doing with preg_match! :)

Thanks,
Ben
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Getting onload="" from <body> tag

Post by ben.artiss »

.... I was very silly :P a slight alteration to what you first suggested works great:

Code: Select all

if (preg_match('/onload="([^"].*?)"/i',$content,$match)) {
    foreach ($match as $test) {
        echo $test.'<br />';
    }
}
 
/*
Output:
onload="test();"
test();
*/
Thanks for your help man.

Regards,
Ben
Post Reply