phpregexapiextractnessus

PHP - Extract data from string with regex


I need help to do this operation. I Have a string like this:

<!doctype html> <html> <head> <meta charset="utf-8"> <title>Formatting the report</title><meta http-equiv="refresh" content="5;url=/file/xslt/download/?fileName=somename.pdf"> </head>

I need to extract the fileName parameter. How to do this?

I thing that is possible with regex, but I do not know well this.

Thanks!


Solution

  • Try this..

    This will capture the filename

    The Pattern is given below

    /fileName=(.+?)\"/
    
    <?php
    $subject = "<!doctype html> <html> <head> <meta charset="utf-8"> <title>Formatting the report</title><meta http-equiv="refresh" content="5;url=/file/xslt/download/?fileName=somename.pdf"> </head>";
    $pattern = '/fileName=(.+)"/';
    preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 2);
    print_r($matches);
    ?>
    

    $1->Contains the file name

    demo