php在最后一个字符串添加GET

i want to add utm_source=twitter in the of the links

i have a link let say http://abcd.com/news?id=1

it need to be http://abcd.com/news?id=1&utm_source=twitter

if http://abcd.com/news/1

it need to be

http://abcd.com/news/1?utm_source=twitter

any idea?

To check if your link already has URL parameters on the end of it, look for the ? character in the URL. If it's there, use a & instead.

$link = 'http://abcd.com/news?id=1'; // or http://abcd.com/news
$join_char = strpos($string, '?') !== -1 ? '&' : '?'; // determine if we need & or ?
$link .= $join_char . 'utm_source=twitter';

If you're simply adding it to the end of a link it would look something like

$link . "?utm_source=twitter";

You can check if the URL already contains a query string and branch your logic accordingly:

if (strpos($url, '?') === FALSE) {
    $url .= '?utm_source=twitter';
} else {
    $url .= '&utm_source=twitter';
}