douxin2002 2017-03-28 19:12
浏览 28
已采纳

从PHP中的帖子数据中获取属性

I'm using laravel to create a simple social network. Users can type @ in the post area to get a list of their friends to mention them. Every mention in a link like this (using zurb/tribute from github)

<a type="mention" href="/user/Jordan" title="Jordan">Jordan</a>

Normal links other than mentions won't have type='mention'

Now when I get the post and insert it into the database I need to get a list of users mentioned in the post. I'm looking for links which have the type ='mention' and if there's any I want to get the title of everyone to insert into the notification system. What PHP code do I need to add in this if statement?

if(stristr(request('post'),' type="mention" ')){

}
  • 写回答

1条回答 默认 最新

  • dougui2254 2017-03-28 19:54
    关注

    Aside from using an AST (Abstract Syntax Tree), your best bet would be to either use DOM on PHP Side, e.g.:

    $string = '<a type="mention" href="/user/Jordan" title="Jordan">Jordan</a>';
    $doc = new \DOMDocument();
    $doc->loadHTML($string);
    $xpath = new \DOMXPath($doc);
    foreach ($xpath->query("//a[@type='mention']") as $a) {
        $href = $a->getAttribute('href');
        $title = $a->getAttribute('title');
        echo sprintf("Found mention of %s with href of %s
    ", $title, $href);
    }
    

    However, I probably wouldn't be sending the A node back to the server. You should consider working out some way to make it a display-only feature implemented on the browser side, and simply send the "@jordan" string back to the server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 优质github账号直接兑换rmb,感兴趣伙伴可以私信
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)