how to extract the onclick value from a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

how to extract the onclick value from a string

Post by jasongr »

Hello

Assuming a string has the value:
onclick="<javascript code here>"
or
onclick='<javascript code here>'

is there an easy method using regular expression to obtain the JavaScript code without resorting to parsing?
onclick is case insensitive and the <javascript code here> may contain double or single quotes, but you can assume it is legal

any help would be appreciated
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

time to get the regex book, friend.

Code: Select all

preg_match_all('#<\s*\w+.*?\s+onclick\s*=\s*(&#1111;"'']?)(.*?)(\\1).*?>#si', $text, $matches);
print_r($matches);
code is untested.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Or, if you need this through javascript, then use a combination of getElementById() and the object's properties.
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

can I obtain the onclick properties of the object via JavaScript?
that would be very nice to know
Post Reply