php教程 php手册 php preg_replace函数基础与实例代码

php preg_replace函数基础与实例代码

Jun 13, 2016 am 11:17 AM
php replace 그리고 암호 기능 베이스 지도 시간

 

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('@<script>]*?>.*?</script>@si',  // strip out javascript教程 www.bkjia.com
'@[^>

);

$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来检索网页,一个正则表达式的组合,以去除所有不必要的空白。这个功能甚至剥夺了从形式和script标记,这是由php函数忽略,如用strip_tags(他们地带唯一的标记文本,留下完整的文字在中间)。

正则表达式被分为两个阶段,以避免删除单(也由 s的匹配)回车,但仍然删除所有空白行和多个换行符或空格,修整手术进行了2个阶段进行。
*/
?>


본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

CakePHP 날짜 및 시간

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

CakePHP 프로젝트 구성

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

CakePHP 파일 업로드

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

CakePHP 라우팅

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP 토론

CakePHP 빠른 가이드 CakePHP 빠른 가이드 Sep 10, 2024 pm 05:27 PM

CakePHP 빠른 가이드

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법

See all articles