tags:

views:

136

answers:

2

I'm trying to split an HTML document into its head and body:

my @contentsArray = split( /<\/head>/is, $fileContents, 1);
if( scalar @contentsArray == 2 ){
    $bodyContents = $dbh->quote(trim($contentsArray[1]));
    $headContents = $dbh->quote(trim($contentsArray[0]) . "</head>"); 
}

is what i have. $fileContents contains the HTML code. When I run this, it doesn't split. Any one know why?

+1  A: 

sorry, figured it out. Thought the 1 was how many times it would find the expression not limit the results. Changed to 2 and works.

Phil Jackson
Don't post non-answers. Update your question or use comments instead.
Sinan Ünür
@Sinan Ünür: looks like an answer to me. I only bothered to post a separate answer because "limit the results" could be interpreted wrongly.
ysth
+6  A: 

The third parameter to split is how many results to produce, so if you want to apply the expression only once, you would pass 2.

Note that this does actually limit the number of times the pattern is used to split the string (to one fewer than the number passed), not just limit the number of results returned, so this:

print join ":", split /,/, "a,b,c", 2;

outputs:

a:b,c

not:

a:b
ysth