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 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问