>  기사  >  백엔드 개발  >  PHP 일반 preg_replace_callback 함수 사용

PHP 일반 preg_replace_callback 함수 사용

怪我咯
怪我咯원래의
2017-07-06 10:40:341706검색

이 글에서는 주로 PHP 정규 preg_replace_callback함수의 사용법을 소개합니다. 예제에서는 정기적 교체를 위한 preg_replace_callback 함수의 관련 기술을 분석합니다. 필요하신 분들은 참고하세요

이 글에서는 PHP 정규 preg_replace_callback 함수의 사용법을 설명합니다. . 참고할 수 있도록 모든 사람과 공유하세요. 구체적인 구현 방법은 다음과 같습니다.

php 정규식은 강력합니다. 이 예에서는 preg_replace_callback 함수

// Define a dummy text, for testing...
$Text = "Title: Hello world!\n";
$Text .= "Author: Jonas\n";
$Text .= "This is a example message!\n\n";
$Text .= "Title: Entry 2\n";
$Text .= "Author: Sonja\n";
$Text .= "Hello world, what's up!\n";
// This function will replace specific matches
// into a new form
function RewriteText($Match){
  // Entire matched section: 
  // --> /.../
  $EntireSection = $Match[0];
  // --> "\nTitle: Hello world!"
  // Key 
  // --> ([a-z0-9]+)
  $Key      = $Match[1];
  // --> "Title"
  // Value 
  // --> ([^\n\r]+)
  $Value    = $Match[2];
  // --> "Hello world!"
  // Add some bold (<b>) tags to around the key to
  return &#39;<b>&#39; . $Key . &#39;</b>: &#39; . $Value;
}
// The regular expression will extract and pass all "key: value" pairs to
// the "RewriteText" function that is definied above
$NewText = preg_replace_callback(&#39;/[\r\n]([a-z0-9]+): ([^\n\r]+)/i&#39;, "RewriteText", $Text);
// Print the new modified text
print $NewText;
의 사용법을 보여줍니다.

위 내용은 PHP 일반 preg_replace_callback 함수 사용의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

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