开发者

preg_replace internal link

开发者 https://www.devze.com 2023-03-20 10:18 出处:网络
how do I do when I want to preg_replace a href, but only if it\'s my own? $a = \'href=\"http://mysite.com/?s=Bananas&lang=en\"\';

how do I do when I want to preg_replace a href, but only if it's my own?

$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"

But what am I doing wrong? This regex-syntax is ve开发者_StackOverflow社区ry difficult...


<?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);
?>

This seems to work for me :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号