2011-07-13 185 views
0

当我想要preg_replace href,但只有它是我自己的时候,我该怎么办?preg_replace内部链接

$a = 'href="http://mysite.com/?s=Bananas&lang=en"'; 
$host = 'http://mysite.com'; 
$a = preg_replace('#href="'.$host.'\/?[(s|p)]=([.*?])&lang=([.*?])"#e','href="index.php#$1\/$2\\lang\/$3"',$a); 
//The result I want: 
echo $a; 
//Becomes href="http://mysite.com/#s/Bananas\\lang/en" 

但是我做错了什么? 此正则表达式的语法是非常困难的......

+1

什么,你到底要不要?请写出你想要的输出。 – Chintan

+0

This:'href =“http://mysite.com/#s/Bananas\\lang/en”' – maxxie

+0

不要在你的正则表达式中使用'e'(PREG_REPLACE_EVAL)修饰符,除非你100%确定你是什么做... – binaryLV

回答

0
<?php 
$a = 'href="http://mysite.com/?s=Bananas&lang=en"'; 
$host = 'http://mysite.com'; 
echo preg_replace('#href="'.preg_quote($host).'/\?(s|p)=(.*?)&lang=(.*?)"#','href="'.$host.'/#$1/$2\\\\\lang/$3"',$a); 
?> 

这似乎为我工作:)