开发者

Why does my use of Perl's split function not split?

开发者 https://www.devze.com 2022-12-19 22:35 出处:网络
I\'m trying to split an HTML document into its head and body: my @contentsArray = split( /<\\/head>/is, $fileContents, 1);

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]));
    $he开发者_开发技巧adContents = $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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消