0

I want to add GET parameter to all URLs in special string (like html content of a website) .

For example :

Before:

$content = '... <a href="http://foo.bar/register.php">register </a> ... <a href="http://foo.bar/login.php?t=1">login</a> ...';

After:

 $content = '... <a href="http://foo.bar/register.php?wid=${wid}">register </a> ... <a href="http://foo.bar/login.php?t=1&wid=${wid}">login</a> ...'; 

I think that this is only done with a regular expression , for this reason I wrote this function :

function makeLinks($str)
{
   $str = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '$1?wid=${wid}', $str);
        return $str;
}

But this pattern having problems! for example :

http://google.com?foo=bar   =>  http://google.com?wid=${wid}?foo=bar

Please help me.

B. Azizan
  • 93
  • 1
  • 4
  • 12
  • possible duplicate of [Manipulate a url string by adding GET parameters](http://stackoverflow.com/questions/5809774/manipulate-a-url-string-by-adding-get-parameters) – PHPExpert Feb 19 '16 at 06:33
  • Why don't you concatenate string with comma or dot ? Just like $url .= '&wid=${wid}'; – vural Feb 19 '16 at 06:37
  • @vural It may be several links in text. For more information I edited my question. – B. Azizan Feb 19 '16 at 07:02
  • @B.Azizan could you please try the function I wrote below. – vural Feb 19 '16 at 07:37

2 Answers2

0

I think there may be a short way. My solution :

function makeLinks($str) {
    preg_match_all('|(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*))|', $str, $urls);

    if ($urls && isset($urls[1])) {
        foreach ($urls[1] as $url) {
            $new_url = $url . (strpos($url, '?') ? '&' : '?') . 'wid=${wid}';

            $str = str_replace($url, $new_url, $str);
        }
    }

    return $str;
}
vural
  • 381
  • 2
  • 11
0

Try This:

function makeLinks($str)
{
   $str = preg_replace_callback('/\b((?:https?|ftp):\/\/(?:[-A-Z0-9.]+)(?:\/[-A-Z0-9+&@#\/%=~_|!:,.;]*)?)(?:\?([A-Z0-9+&@#\/%=~_|!:,.;]*))?/i', 'modify_url', $str);
   return $str;
}
function modify_url($matches) {
    $query = isset($matches[2]) ? $matches[2]:'';
    $result = $matches[1].'?'.$query;
    if(!empty($query)) $result .= '&';
    return $result.'wid=${wid}';
}

Optionally you can just add @ without affecting the ending result. I hate to use them, but here it is in case you want to use them:

function modify_url($matches) {
    $result = $matches[1].'?'.@$matches[2];
    if(!@empty($matches[2])) $result .= '&';
    return $result.'wid=${wid}';
}

Idealy, you should extract the urls and parse them, but this solution should work.

Valerie
  • 332
  • 2
  • 9
  • thanks a lot dear @val. this solution worked . but URLs that do not have http [ OR https OR ftp] Not detected. Some of URLs may only have a www . – B. Azizan Feb 19 '16 at 08:15
  • no problem, try using this instead: preg_replace_callback('/\b((?:(?:https?|ftp):\/\/|www\.)(?:[-A-Z0-9.]+)(?:\/[-A-Z0-9+&@#\/%=~_|!:,.;]*)?)(?:\?([A-Z0-9+&@#\/%=~_|!:,.;]*))?/i', 'modify_url', $str); – Valerie Feb 19 '16 at 08:21