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条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图