PHP编程:php正则preg_replace_callback函数用法实例
发布时间:2020-12-13 02:39:17 所属栏目:PHP教程 来源:网络整理
导读:《php正则preg_replace_callback函数用法实例》要点: 本文介绍了php正则preg_replace_callback函数用法实例,希望对您有用。如果有疑问,可以联系我们。 本篇章节讲解php正则preg_replace_callback函数的用法.供大家参考研究.具体实现办法如下: PH
《php正则preg_replace_callback函数用法实例》要点: 本篇章节讲解php正则preg_replace_callback函数的用法.分享给大家供大家参考.具体实现办法如下:PHP应用 php正则表达式功能强大,本范例演示了preg_replace_callback函数的用法 // Define a dummy text,for testing... $Text = "Title: Hello world!n"; $Text .= "Author: Jonasn"; $Text .= "This is a example message!nn"; $Text .= "Title: Entry 2n"; $Text .= "Author: Sonjan"; $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 // --> ([^nr]+) $Value = $Match[2]; // --> "Hello world!" // Add some bold (<b>) tags to around the key to return '<b>' . $Key . '</b>: ' . $Value; } // The regular expression will extract and pass all "key: value" pairs to // the "RewriteText" function that is definied above $NewText = preg_replace_callback('/[rn]([a-z0-9]+): ([^nr]+)/i',"RewriteText",$Text); // Print the new modified text print $NewText; 希望本文所述对大家的php程序设计有所赞助. 编程之家培训学院每天发布《php正则preg_replace_callback函数用法实例》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |