Hi,
I have this simple expression that retrieves the content from the HTML head.
Since I don't have much experience with expressions, I'm not sure whether this is all that efficient.Code:/.*<head>(.*)<\/head>.*/
Any suggestions?
Thanks.
Printable View
Hi,
I have this simple expression that retrieves the content from the HTML head.
Since I don't have much experience with expressions, I'm not sure whether this is all that efficient.Code:/.*<head>(.*)<\/head>.*/
Any suggestions?
Thanks.
you only really need this:
I added two modifiers: the 'i' modifier so that it would be treated as case insensitive (eg. HEAD, Head), and the 's' modifier to turn on single-line mode so that the single-character (".") also matches line-breaks.Code:/<head>(.*)<\/head>/si
Avoid using .* where not necessary or if there's a practical alternative that's lazy (as opposed to greedy; use excluders, not includers). Instead of the .* in kows' sample, maybe you could use a "not </head>". But my example isn't working. :)
Or you could do it without regex:PHP Code:function getHead($HTML) {
$start = stripos($HTML, "<HEAD>") + 6;
if($start) {
$stop = stripos($HTML, "</HEAD>", $start);
if($stop) {
return substr($HTML, $start, $stop - $start);
}
}
return "";
}
Depending on how you want it done you could even use the strip_tags PHP function.
No. But had you looked on the page, there is a function on there that does. Such as:
... and it also makes for a pretty interesting demo of RegExp too.PHP Code:function strip_selected_tags($str, $tags = "", $stripContent = false)
{
preg_match_all("/<([^>]+)>/i", $tags, $allTags, PREG_PATTERN_ORDER);
foreach ($allTags[1] as $tag) {
$replace = "%(<$tag.*?>)(.*?)(<\/$tag.*?>)%is";
$replace2 = "%(<$tag.*?>)%is";
echo $replace;
if ($stripContent) {
$str = preg_replace($replace,'',$str);
$str = preg_replace($replace2,'',$str);
}
$str = preg_replace($replace,'${2}',$str);
$str = preg_replace($replace2,'${2}',$str);
}
return $str;
}
What you said -- that you could use strip_tags() -- is still incorrect.
If you're going to point out that a function posted in the comments of the strip_tags() documentation might be useful, then you should probably say that it actually has nothing to do with strip_tags() and link to the comment itself. Otherwise, you're just misinforming.
Should I even mention that the function you posted doesn't even do what was needed (or what you suggested it did), anyway? It was written that way to support self-closing tags (like <input />).