SupraCharger Posted March 1, 2013 Share Posted March 1, 2013 (edited) Hi, I'm trying to pull out parts of a html page and I've tried this for hours an I can't seem to get it to work. // I'm pulling this info from get_file_contents () $contfile = ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <!-- PHP: Header End --> <!-- PHP: Footer Start --> </body> </html> '; $pattern = '/^(.*)(<!\-\- PHP: Header End \-\->)(.*)$/'; $replacement = "$1$2"; $header = preg_replace ($pattern, $replacement, $contfile); // I want it to output $header = ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <!-- PHP: Header End --> '; I've tried to use other functions (preg_grep () , preg_match () ) and I can't get those to work. If you could please show me the syntax to how to properly accomplish this task It would be greatly appreciated. Thanx for your help, Andrew Edited March 1, 2013 by SupraCharger Quote Link to comment https://forums.phpfreaks.com/topic/275075-need-help-with-preg-functions-to-make-a-header-footer/ Share on other sites More sharing options...
Solution teynon Posted March 1, 2013 Solution Share Posted March 1, 2013 http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/ Look at the right for "." - Any character except newline. Try this: ^([\d\D]*)(<!\-\- PHP: Header End \-\->)([\d\D]*)$ Quote Link to comment https://forums.phpfreaks.com/topic/275075-need-help-with-preg-functions-to-make-a-header-footer/#findComment-1415754 Share on other sites More sharing options...
SupraCharger Posted March 1, 2013 Author Share Posted March 1, 2013 Thank you that fixed the issue, Andrew Quote Link to comment https://forums.phpfreaks.com/topic/275075-need-help-with-preg-functions-to-make-a-header-footer/#findComment-1415755 Share on other sites More sharing options...
Christian F. Posted March 1, 2013 Share Posted March 1, 2013 (edited) The simpler version of that expressions would be as follows: $RegExp = '/^.*?PHP: Header End -->/us';"u" to make sure it's UTF-8 compatible, and "s" to make the dot match newlines as well. Hyphens only have a special meaning inside a character class, and the greater-than sign only in named groups and lookarounds. Of course, if in doubt it doesn't really hurt to escape them. Of course, that said this could have been done quite simply with a strpos and substr combination. Edited March 1, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/275075-need-help-with-preg-functions-to-make-a-header-footer/#findComment-1415763 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.