* $str:任意字符串
* $start:要替取的字符串之前的字符串
* $end:要替取的字符串之前的字符串
* $add:提取出来的结果是否加上$start和$end

* 返回数组
//例子
$html='
<!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" dir="ltr" lang="en-US">
<head profile="http://gmpg.org/xfn/11">
<title>记录与PHP的PK经历 / PK with php!</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="screen,projection" href="http://www.goalercn.com/wp-content/themes/plaintxtblog/style.css" title="plaintxtBlog" />
<link rel="stylesheet" type="text/css" media="print" href="http://www.goalercn.com/wp-content/themes/plaintxtblog/print.css" />
<link rel="alternate" type="application/rss+xml" href="http://www.goalercn.com/feed/" title="记录与PHP的PK经历 RSS feed" /><link rel="alternate" type="application/rss+xml" href="http://www.goalercn.com/comments/feed/" title="记录与PHP的PK经历 comments RSS feed" />
<link rel="pingback" href="http://www.goalercn.com/xmlrpc.php" />
';

//获取title
print_r(inContent($html,'<title>','</title>',false));
/*
返回:
Array
(
[0] => 记录与PHP的PK经历 / PK with php!
)
*/

print_r(inContent($html,'<title>','</title>',true));
/*
返回:
Array
(
[0] => <title>记录与PHP的PK经历 / PK with php!</title>
)
*/

print_r(inContent($html,'href="','"',false));
/*
返回:
Array
(
[0] => http://www.goalercn.com/wp-content/themes/plaintxtblog/style.css
[1] => http://www.goalercn.com/wp-content/themes/plaintxtblog/print.css
[2] => http://www.goalercn.com/feed/
[3] => http://www.goalercn.com/comments/feed/
[4] => http://www.goalercn.com/xmlrpc.php
)
*/
PHP 代码:复制
function inContent($str,$start,$end,$add = true) { $first = strstr($str,$start); if ($first == false) { return array(); } else { $first = substr($first,strlen($start)); } $second = strstr($first,$end); if ($second == false) { if ($add) { return array($start.$first); } else { return array($first); } } $final = substr($first,0,-strlen($second)); if ($add) { $final = $start.$final.$end; } $result[] = $final; $second = substr($second,strlen($end)); if (strstr($second,$start)) { $result = array_merge($result,inContent(substr($second,strlen($end)),$start,$end,$add)); } return $result; }