本帖最后由 leon_studio 于 2009-5-4 21:04 編輯 1.采集表格中的內容為數組 function get_td_array($table) { ? ?? ???$table = preg_replace("'<table[^>]*?>'si","",$table); ? ?? ???$table = preg_replace("'<tr[^>]*?>'si","",$table); ? ?? ???$table = preg_replace("'<td[^>]*?>'si","",$table); ? ?? ???$table = str_replace("</tr>","{tr}",$table); ? ?? ???$table = str_replace("</td>","{td}",$table); ? ?? ???//去掉 HTML 標記 ? ?? ???$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);? ? ?? ???//去掉空白字符 ? ?? ???$table = preg_replace("'([rn])[s]+'","",$table); ? ?? ???$table = str_replace(" ","",$table); ? ?? ???$table = str_replace(" ","",$table); ? ?? ???$table = explode('{tr}', $table); ? ?? ???array_pop($table); //OSPHP.com.CN ? ?? ???foreach ($table as $key=>$tr) { ? ?? ?? ?? ?? ? $td = explode('{td}', $tr); ? ?? ?? ?? ?? ? array_pop($td); ? ?? ?? ?? ?$td_array[] = $td; ? ?? ???} ? ?? ???return $td_array; } 復制代碼 2.取得某行代碼中間的字符串 /** +---------------------------------------------------------- * 取得某行代碼之間的字符串 +---------------------------------------------------------- * 例: echo get_innerhtml("<tr><td height=20>something</td></tr>", "td");? ?//will print "something". +---------------------------------------------------------- */ function get_innerhtml($html,$label) { ? ? $result_arr = preg_split("/<\/".$label.">/i",$html); ? ? $pattern = "/<".$label.".*?>/i"; ? ? for ($i = 0; $i < count($result_arr); $i++) { ? ?? ???list($left, $right) = preg_split($pattern,$result_arr[$i],2); ? ?? ???$result_arr[$i] = $right; ? ? } ? ? return $result_arr; } 復制代碼 3.獲取Input的HTML代碼中的Value值 //獲取Input的HTML代碼中的Value值 function get_input_value($input) { ? ? $pos = stripos($input, "value=") + 6; ? ? if ($pos !== false) { ? ?? ???$input = substr($input, $pos); ? ?? ???if (substr($input, 0, 1) == "\"") ? ?? ?? ?? ?return substr($input, 1, strpos($input, "\"", 1) - 1); ? ?? ???else ? ?? ?? ?? ?return substr($input, 0, strpos($input, " ") - 1); ? ? } ? ? return false; } 復制代碼 4.獲取字符串$str中,字符串$a與字符串$b之間的字符串 //獲取字符串$str中,字符串$a與字符串$b之間的字符串 function getcontentbetween($a, $b, $str) { ? ? if ($str!=="" && $a!=="" && $b!=="") { ? ?? ???$start = strpos($str, $a) + strlen($a); ? ?? ???return substr($str, $start, strpos($str, $b, $start + 1) - $start); ? ? } ? ? return false; } 復制代碼 5.<br>變成回車 function br2nl($text) { ? ?return??trim(preg_replace('/<br\s*/?'.'>/i', '', $text)); } 復制代碼 6.換行奕成<p></p> function nl2p($text) { return str_replace(array("\r\n\r\n","\r\n","\r","\n"),"</p><p>",$text); } 復制代碼 7.獲得當前腳本網址 function get_php_url(){? ? ?? ???if(!empty($_SERVER["REQUEST_URI"])){? ? ?? ?? ?? ?? ? $scriptName = $_SERVER["REQUEST_URI"];? ? ?? ?? ?? ?? ? $nowurl = $scriptName;? ? ?? ???}else{? ? ?? ?? ?? ?? ? $scriptName = $_SERVER["HP_SELF"];? ? ?? ?? ?? ?? ? if(empty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName;? ? ?? ?? ?? ?? ? else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];? ? ?? ???}? ? ?? ???return $nowurl;? } 復制代碼 8.去除html標記 function Text2Html($txt){ ? ?? ???$txt = str_replace("??"," ",$txt); ? ?? ???$txt = str_replace("<","<",$txt); ? ?? ???$txt = str_replace(">",">",$txt); ? ?? ???$txt = preg_replace("/[\r\n]{1,}/isU","<br/>\r\n",$txt); ? ?? ???return $txt; } 復制代碼 9.相對路徑轉絕對路徑 function relative_to_absolute($content, $feed_url) {? ? ? preg_match('/(http|https|ftp):\/\//', $feed_url, $protocol);? ? ? $server_url = preg_replace("/(http|https|ftp|news):\/\//", "", $feed_url);? ? ? $server_url = preg_replace("/\/.*/", "", $server_url);? ? ? if ($server_url == '') {? ? ?? ???return $content;? ? ? }? ? ? if (isset($protocol[0])) {? ? ?? ???$new_content = preg_replace('/href="\//', 'href="'.$protocol[0].$server_url.'/', $content);? ? ?? ???$new_content = preg_replace('/src="\//', 'src="'.$protocol[0].$server_url.'/', $new_content);? ? ? } else {? ? ?? ???$new_content = $content;? ? ? }? ? ? return $new_content;? } 復制代碼 10.取得所有鏈接 function get_all_url($code){? ? ?? ???preg_match_all('/<a\s+href=["|\']?([^>"\' ]+)["|\']?\s*[^>]*>([^>]+)<\/a>/i',$code,$arr);? ? ?? ???return array('name'=>$arr[2],'url'=>$arr[1]);? } 復制代碼 11.獲取指定標記中的內容 function get_tag_data($str, $start, $end){? ? ?? ???if ( $start == '' || $end == '' ){? ? ?? ?? ?? ?? ?return;? ? ?? ???}? ? ?? ???$str = explode($start, $str);? ? ?? ???$str = explode($end, $str[1]);? ? ?? ???return $str[0];? } 復制代碼 |