在每个类别后添加逗号

this seems like a pretty straight forward question and for some reason I am having trouble figuring out how to achieve what I am looking for.

$category = $html2->find('.video_cats',0);
$category = $genre->plaintext;


<div class="video_cats">
<span>Categories:</span>
    <a href="http://www.example.com" title="Example Category" class="video_cat">House</a>   
    <a href="http://www.example.com" title="Example Category" class="video_cat">The Cat</a> 
    <a href="http://www.example.com" title="Example Category" class="video_cat">Car</a> 
    <a href="http://www.example.com" title="Example Category" class="video_cat">The Dog</a>                                                             
</div>

Currently I have a string called $category if we print the results of this string it will return the following text One Two Three Four Five I am trying to make it so that it returns the text One, Two, Three, Four, Five. Any help is greatly appreciated.

EDIT : This is the desired output Category: A Cat, Dog, A Horse, House because some of the category names have spaces in them we can't just replace the spaces with commas.

Search for the anchors rather than the DIV, then you can loop over them and make an array.

$cat_array = array():
foreach ($html2->find(".video_cat") as $cat) {
    $cat_array[] = $cat->plaintext;
}

$category = implode(', ', $cat_array);

You could use explode and implode like this:

$category = implode(", ", explode(" ", $category));

Then just print out the new contents of $category.

After taking your edit into consideration, the above would not really work, however if you were to use split instead of explode you could use regular expressions to do a backreference match on a list of pre-defined words such as: "A", "The", etc... (See: http://www.regular-expressions.info/refcapture.html )

This could be a solution, but is still rather problematic. The best approach would be to find a way to delimit your list better.