通过php在字符串中添加一个url

I have a string

$string = '<a href="a.php">a</a>,<a href="b.php">b</a>';

I want to add url http://www.example.com before after href=" , that means it will be <a href="http://www.example.com/a.php">

I have tried other functions like str_ireplace , str_replace . I want to do it by php preg_match()

I can do easily by 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>

Can any one help me to do it by php?

You looking for this:

$strink = "http://www.example.com";
<a href="'.$string.'a.php"></a>

If you want to use preg_match(), try this:

preg_replace ('/href=\"/', '$0http://www.example.com/', $string);

I'm not sure about the second argument, tough. Might need some escaping... See here for some examples: http://php.net/manual/en/function.preg-replace.php

You can use str_replace :

$string = '<a href="a.php">';
$url = 'href="http://www.example.com/';    
echo str_replace('href="', $url, $string);

If your string is already defined somewhere else (for example: from a database), a simple way is str_ireplace (case-insensitive str_replace):

$string = '<a href="a.php">';
$string = str_ireplace('href="', 'href="http://www.example.com/', $string);

I'm using a string that I know exists href=" and replacing it with href="http://www.example.com/