我有一个字符串
$string = '<a href="a.php">a</a>,<a href="b.php">b</a>';
我想添加urlhttp://www.example.com在href="之前,这意味着它将是<a href="http://www.example.com/a.php">
我尝试过其他函数,如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>
有人能帮我用php做这件事吗?
您正在寻找这个:
$strink = "http://www.example.com";
<a href="'.$string.'a.php"></a>
如果您想使用preg_match()
,请尝试以下操作:
preg_replace ('/href='"/', '$0http://www.example.com/', $string);
我不确定第二个论点,强硬。可能需要逃跑。。。有关一些示例,请参见此处:http://php.net/manual/en/function.preg-replace.php
您可以使用str_replace:
$string = '<a href="a.php">';
$url = 'href="http://www.example.com/';
echo str_replace('href="', $url, $string);
如果您的字符串已经在其他地方定义(例如:从数据库),一种简单的方法是str_ireplace
(不区分大小写的str_replace):
$string = '<a href="a.php">';
$string = str_ireplace('href="', 'href="http://www.example.com/', $string);
我正在使用一个我知道存在href="
的字符串,并将其替换为href="http://www.example.com/