SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to extract the onclick value from a string

    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

  2. #2
    SitePoint Addict psyon's Avatar
    Join Date
    May 2004
    Location
    Cedar Rapids
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    somethign like
    Code:
    preg_match("/onclick=[\"\'](.*?)[\'\"]/i", $string, $match);
    $javascript = $match[1];

  3. #3
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this
    PHP Code:
    $onClick 'onClick="javascript code here"';
        if(
    preg_match('/onclick=(\'|")(.*)(\'|")/i',$onClick,$match))
            
    print_r($match); 

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IMHO, it should look like
    PHP Code:
    $html = <<<EOF
    <a href="...." onclick = "if('abc')alert ('de\'f')">a</a>
    EOF;

    preg_match_all(
        
    "~onclick\s*=\s*([\"\'])(.*?)\\1~si"
        
    $html
        
    $matches);
    var_dump($matches); 

  5. #5
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help people

    I also got this useful answer from NogDog a few minutes ago
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd"> 
    <html lang='en'> 
    <head> 
    <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'> 
    <title>Untitled</title> 
    <style type="text/css"> 
    </style> 
    </head> 
    <body> 
    
    <?php 
    $test = <<<EOD 
    <submit value='submit' onClick="this.is.a.('test');it.is.only.a('test');"> 
    EOD; 
    echo "<p>Initial string: <strong>". htmlentities($test) . "</strong></p>\n"; 
    preg_match("/onclick=[\"\'](.*;)[\"\']/i", $test, $matches); 
    echo "<p>Result of preg_match: <strong>{$matches[1]}</strong></p>\n"; 
    ?> 
    
    </body> 
    </html>

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    Mars
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I liked the psyon's code

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd suggest you test all expressions posted above with this (valid) html snippet
    PHP Code:
    $html = <<<EOF
    <a href="...." onclick = "if('test1')
        alert('te\'st1')">a</a>
    <a href="...." onclick = 'if("test2")
        alert("te\"st2")'>b</a>
    EOF; 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •