通过php在字符串中添加url
我有一根绳子通过php在字符串中添加url,php,preg-replace,Php,Preg Replace,我有一根绳子 $string = '<a href="a.php">a</a>,<a href="b.php">b</a>'; $string=','; 我想在href=”之后添加url,这意味着它将是 我尝试过其他函数,比如str_ireplace、str_replace。我想用php preg_match()实现它 我可以很容易地用jquery完成 <script type="text/javascript"> jQuery(
$string = '<a href="a.php">a</a>,<a href="b.php">b</a>';
$string=',';
我想在href=”之后添加url,这意味着它将是
我尝试过其他函数,比如str_ireplace、str_replace。我想用php preg_match()实现它
我可以很容易地用jquery完成
<script type="text/javascript">
jQuery('.scroller a').each(function(){
var url = jQuery(this).attr('href');
var newUrl = 'www.example.com' +url;
jQuery(this).attr('href', newUrl);
});
jQuery('.scroller img').each(function(){
var url = jQuery(this).attr('src');
var newUrl = 'www.example.com' +url;
jQuery(this).attr('src', newUrl);
});</script>
jQuery('.scroller a')。每个(函数(){
var url=jQuery(this.attr('href');
var newUrl='www.example.com'+url;
jQuery(this.attr('href',newUrl);
});
jQuery('.scroller img')。每个(函数(){
var url=jQuery(this.attr('src');
var newUrl='www.example.com'+url;
jQuery(this.attr('src',newUrl));
});
有人能帮我用php吗?你在找这个:
$strink = "http://www.example.com";
<a href="'.$string.'a.php"></a>
$strink=”http://www.example.com";
如果要使用preg\u match()
,请尝试以下操作:
preg_replace ('/href=\"/', '$0http://www.example.com/', $string);
我不确定第二个论点,很难。可能需要一些逃避。。。
请参见此处的一些示例:您可以使用:
$string='';
$url='href=”http://www.example.com/';
echo str_replace('href=“”,$url,$string);
如果您的字符串已经在其他地方定义(例如:从数据库中),一种简单的方法是str\u-ireplace
(不区分大小写的str\u-replace):
$string='';
$string=str_ireplace('href=“”,'href=”http://www.example.com/“,$string);
我正在使用一个我知道存在的字符串
href=“
并将其替换为href=”http://www.example.com/
对于投票人来说,preg\u match()
有什么具体原因吗?对我来说,这是个有效的问题!我已经尝试过其他函数,所以我认为preg\u match()会做得很好
$string = '<a href="a.php">';
$url = 'href="http://www.example.com/';
echo str_replace('href="', $url, $string);
$string = '<a href="a.php">';
$string = str_ireplace('href="', 'href="http://www.example.com/', $string);