dongxiezhi9564 2011-03-25 10:52
浏览 69
已采纳

使用php解析javascript文本

Greetings! I have heard of various methods to capture and return text. For example regular expressions. I am slightly new to PHP but I was wondering how I would capture and return text from this JavaScript code using PHP. I am only interested in capturing msgBody, msgTitle, and insertDate. Thank you for looking my my problem

function wibiyaNotifierLoad() {
    if (typeof(jQuery.cookie) == 'function') {
        loadjscssfile("Graphics_Toolbar/Notifier/notifier_2.css?v2", "css", "head");
        var data = {
            "count": 1,
            "notifications": [{
                "Id": "264727",
                "toolbarId": "602582",
                "msgId": "9",
                "msgTitle": "Its that time of year!",
                "msgBody": "check you email for updates!",
                "msgLink": "",
                "msgImage": "",
                "filter": "",
                "startDate": "2011-03-22 23:00:00",
                "expirationDate": "2011-03-23 00:00:00",
                "active": "1",
                "insertDate": "2011-03-22 23:12:31"
            }],
            "ok": true
        };
  • 写回答

4条回答 默认 最新

  • dongnuo3749 2011-03-25 10:59
    关注

    When you say parsing, you should look into J4P5. But you can probably get away with an inexact solution and regular expressions here. To capture the text and then extract individual values:

    preg_match('#(\[\{.*?\}\])#s', $javascript, $match);
    $values = json_decode($match[1], TRUE);
    
    $msgBody = $values[0]["msgBody"];
    

    Could make the regex more explicit by starting it with '#"notifications": (\[\{ however.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料