php preg_replace函數基礎與執行個體代碼_PHP教程

來源:互聯網
上載者:User
php教程 preg_replace函數基礎與執行個體代碼
//preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) 主題為匹配搜尋模式,替換替換
/*
要搜尋的模式。它可以是一個字串或一個字串數組。

電子修飾符使preg_replace函數()替代治療後,適當引用作為參數是php教程代碼進行替換。提示:請確保置換構成一個有效php代碼字串,否則php將抱怨在包含preg_replace函數線()解析錯誤。

傳回值

preg_replace函數()返回一個數組,如果這個問題的參數是一個數組或一個字串,否則。

如果找到匹配,新問題會產生,否則將返回主題不變或null如果發生錯誤。
*/

//執行個體一

$string = 'april 15, 2003';
$pattern = '/(w+) (d+), (d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);

//執行個體二

$string = 'the quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);

//通過ksorting模式和替代,我們應該得到我們想要的。

ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);

//更換幾個值

$patterns = array ('/(19|20)(d{2})-(d{1,2})-(d{1,2})/',
'/^s*{(w+)}s*=/');
$replace = array ('3/4/12', '$1 =');
echo preg_replace($patterns, $replace, '{startdate} = 1999-5-27');

//過濾所有html 標籤

preg_replace("/(]*>)/e",
"'1'.strtoupper('2').'3'",
$html_body);

//過濾所有script代碼

$user_agent = "mozilla/4.0 (compatible; msie 5.01; windows nt 5.0)";

$ch = curl_init(); // initialize curl handle
curl_setopt($ch, curlopt_url, $url); // set url to post to
curl_setopt($ch, curlopt_failonerror, 1); // fail on errors
curl_setopt($ch, curlopt_followlocation, 1); // allow redirects
curl_setopt($ch, curlopt_returntransfer,1); // return into a variable
curl_setopt($ch, curlopt_port, 80); //set the port number
curl_setopt($ch, curlopt_timeout, 15); // times out after 15s

curl_setopt($ch, curlopt_useragent, $user_agent);

$document = curl_exec($ch);

$search = array('@@si', // strip out javascript教程 www.bkjia.com
'@]*?>.*?@siu', // strip style tags properly
'@<[/!]*?[^<>]*?>@si', // strip out html tags
'@@', // strip multi-line comments including cdata
'/s{2,}/',

);

$text = preg_replace($search, "n", html_entity_decode($document));

$pat[0] = "/^s+/";
$pat[2] = "/s+$/";
$rep[0] = "";
$rep[2] = " ";

$text = preg_replace($pat, $rep, trim($text));

return $text;
}

/*
此函數接受一個url並返回頁面的純文字版本。它使用curl來檢索網頁,一個Regex的組合,以去除所有不必要的空白。這個功能甚至剝奪了從形式和script標記,這是由php函數忽略,如用strip_tags(他們地帶唯一的標記文本,留下完整的文字在中間)。

Regex被分為兩個階段,以避免刪除單(也由 s的匹配)斷行符號,但仍然刪除所有空白行和多個分行符號或空格,修整手術進行了2個階段進行。
*/
?>


http://www.bkjia.com/PHPjc/445452.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445452.htmlTechArticlephp教程 preg_replace函數基礎與執行個體代碼 //preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int $count ]] ) 主題為匹配搜尋模...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.