doubi4814 2016-06-10 22:09
浏览 16
已采纳

在每个类别后添加逗号

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.

  • 写回答

2条回答 默认 最新

  • dongqiao2077 2016-06-10 22:33
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?