从标记创建自动链接后,过多的逗号替换问题

My issue is inside the if statement below. My tags structure is like this: tag1, tag2, tag3

I achieved auto linking of each tag of each article but if the number of tags is more than 1, I need to replace two characters that are 1 comma+1 space character and inside the related <p></p> couple. These 2 characters {1 comma+1 space} are before the last </p> tag. Explicitly I mean:

My input is (html source):

<p>Etiketler: <a href="http://localhost/türkçe/etiketler/tag1">tag1</a>, <a href="http://localhost/türkçe/etiketler/tag2">tag2</a>, </p>

and my desired output is (html source)

<p>Etiketler: <a href="http://localhost/türkçe/etiketler/tag1">tag1</a>, <a href="http://localhost/türkçe/etiketler/tag2">tag2</a></p>

my custom function is below> Now, you see the preg_replace but I also tried str_replace. My encoding is utf-8 if it helps. I read the php.net manual for these 2 functions but I couldn't get rid of those 2 characters.

Can you help me please?

function etiketleri_link_yap ($article_tags)
{
    $array_of_tags = explode(", ", $article_tags);
    if (count($array_of_tags) >= 2) 
        {
            $ely ='';
            for ($i = 0; $i < count($array_of_tags); $i++) 
                {
                    $ely .= '<a href="'.sitenin_koku.'türkçe/etiketler/'.$array_of_tags[$i].'">'.$array_of_tags[$i].'</a>, ';
                }
            $arabunu ="/<\/a>, <\/p>/"; $degistir  = '</a></p>';
            $ely = preg_replace($arabunu, $degistir, $ely);
            return $ely;
        }
    else
        {
            $ely ='<a href="'.sitenin_koku.'türkçe/etiketler/'.$array_of_tags[0].'">'.$array_of_tags[0].'</a>';
            return $ely;
        }
}

There is no closing <p> tag in your $ely variable, just <a> tags so your regex never matches.

I would build an array of tags / tag links and implode them at the end:

$links = array();
for ($i = 0; $i < count($array_of_tags); $i++) 
{
   $links[] = '<a href="'.sitenin_koku.'türkçe/etiketler/'.$array_of_tags[$i].'">'.$array_of_tags[$i].'</a>';
}
$ely = implode(', ', $links);

By the way, I would also use a foreach to loop over $array_of_tags but if you are sure the index starts at 0 and there are no missing indices, the result should be the same.