Question

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!

Was it helpful?

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

OTHER TIPS

Try something along the lines of:

$str = '<!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>';

preg_match('@fileName=(.*)"@', $str, $matches);

print_r($matches);

php simple html dom is clean and good way for trace html and find html elements by selector's like Jquery selectors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top