Home > Backend Development > PHP Tutorial > php的preg_replace使用

php的preg_replace使用

WBOY
Release: 2016-06-20 12:26:28
Original
1038 people have browsed it

现有xml文件内容如下:

...<group>			<title></title>			<value>35</value>			<label>$35.00K</label>			<link>index.php?module=Opportunities&action=index&query=true&searchFormTab=advanced_search&lead_source=</link>			<subgroups>				<group>					<title>Prospecting</title>					<value>0</value>					<label>$0.00K</label>					<link>index.php?module=Opportunities&action=index&query=true&searchFormTab=advanced_search&lead_source=&sales_stage=Prospecting</link>				</group>				<group>					<title>Qualification</title>					<value>0</value>					<label>$0.00K</label>					<link>index.php?module=Opportunities&action=index&query=true&searchFormTab=advanced_search&lead_source=&sales_stage=Qualification</link>				</group>....
Copy after login

有如下一段方法,处理xml的,替换节点link的内容,将里面的链接内容用urlencode编码:
function processXML($xmlFile) {		if(!file_exists($xmlFile)) {			$GLOBALS['log']->debug("Cannot open file ($xmlFile)");		}		$pattern = array();		$replacement = array();		$content = file_get_contents($xmlFile);		$content = $GLOBALS['locale']->translateCharset($content,'UTF-16LE', 'UTF-8');               //这行有问题		$pattern[] = '/\<link\>([a-zA-Z0-9#?&%.;\[\]\/=+_-\s]+)\<\/link\>/e';		$replacement[] = "'<link>'.urlencode(\"$1\").'</link>'";		return preg_replace($pattern,$replacement, $content);	}
Copy after login

上面代码的正则表达式在php5.4中是可以的,但是5.5以上版本取消了e参数。我尝试用preg_replace_callback改写,但失败了,preg_replace_callback的代码如下:
$content = preg_replace_callback(			'|<link>([a-zA-Z0-9#?&%.;\[\]\/=+_-\s]+)<\/link>|',			function ($matches) {				$u = urlencode($matches[1]);				return "<link>".$u."</link>";			},			$content		);
Copy after login

运行是有如下错误:Warning: preg_replace_callback(): Compilation failed: invalid range in character class at offset 34

该怎么修改呢,系统环境是php5.6.21


回复讨论(解决方案)

$content = preg_replace_callback(            '/\<link\>(.+?)\<\/link\>/',            function ($matches) {                return "<link>".urlencode($matches[1])."</link>";            },            $content        );
Copy after login
Copy after login

$content = preg_replace_callback(            '/\<link\>(.+?)\<\/link\>/',            function ($matches) {                return "<link>".urlencode($matches[1])."</link>";            },            $content        );
Copy after login
Copy after login

试了下,没匹配到...同样的正则,我在那种在线正则测试网页上试了是可以的,但是php的这个方法就不行

测试了你的代码,并没有发现不对的地方
可能是你贴错了吧

测试了你的代码,并没有发现不对的地方
可能是你贴错了吧


你那php多少版本,内容都对的,一直没成功

php5.4.31 和 php5.6.13 ,测试了,都没有问题
我测试的你贴出的代码,不排除你在粘贴前有非法字符被 CSDN 吃掉了

不过你的 
'|([a-zA-Z0-9#?&%.;\[\]\/=+_ -\s]+)|'
确实写的不对!
- 在方括号中表示区间,如果是 - 这个字符,应该将其写在最

Compilation failed: invalid range in character class 编译失败:字符类中无效的范围
\s 可表示  空格、制表符、回车、换行
那么 _-\s 应表示一个什么样的字符区间呢?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template