dumdumsareyum Posted February 27, 2009 Share Posted February 27, 2009 just not for me I'm trying to match everything between the head tags of an html document and remove it, including the tags. Here's what I have $newData = preg_replace("/(<head>).*(<\/head>)/","", $theData); but it's not working....Help please? Quote Link to comment https://forums.phpfreaks.com/topic/147110-solved-simple-match/ Share on other sites More sharing options...
nrg_alpha Posted February 27, 2009 Share Posted February 27, 2009 $newData = preg_replace('#<head>.*?</head>#si','', $theData); Whenever you use .*, it is a) wiser to make it a lazy quantifier by doing .*? (see this thread to understand why.. Response #11 and #14 in particular. It partly deals with .+ as well as .*, but the principals are the same). and b) since there is a possibility that the head tags in question are separated by many lines of other tags, and since you are using the dot match all, you should use the s modifier after the closing delimiter so that dot match all include newlines. I added the i modifier as well in my example, just in case you run into <HEAD>...</HEAD> as opposed to <head>...</head>, which means it's case insensitive. Quote Link to comment https://forums.phpfreaks.com/topic/147110-solved-simple-match/#findComment-772349 Share on other sites More sharing options...
dumdumsareyum Posted February 27, 2009 Author Share Posted February 27, 2009 Thanks! I'm definitely going to get a copy of the book mentioned in that post. Regex just goes right over my head right now. Quote Link to comment https://forums.phpfreaks.com/topic/147110-solved-simple-match/#findComment-772825 Share on other sites More sharing options...
nrg_alpha Posted February 27, 2009 Share Posted February 27, 2009 Regex is not really hard.. just tricky at first.. but yeah, that book will really puts things into perspective for you (not to mention make your regex life a hell of a lot easier too). Good choice. Quote Link to comment https://forums.phpfreaks.com/topic/147110-solved-simple-match/#findComment-772839 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.