preg_match在脚本php中不起作用,但在线工作

时间:2014-04-25 14:57:52

标签: php regex

我需要得到一个字符串的所有出现,所以我做了一个正则表达式,它在http://www.functions-online.com/preg_match.html上工作,但是当我在脚本php中执行此正则表达式时,它不起作用,我不知道为什么。< / p>

这是我在http://www.functions-online.com/preg_match.html上使用的正则表达式:

'input type=\\"hidden\\" name=\\"invitation\\" value=\\"(\w+)\\"'

这是我在我的脚本php中使用的行(我只是添加分隔符'/'):

 preg_match('/input type=\\"hidden\\" name=\\"invitation\\" value=\\"(\w+)\\"/', $output, $regs);

字符串:

 {"data": {}, "actions": [["html", "\u003cdiv id=\"invites-container\"\u003e\u003cdiv class=\"invitation-table-container\"\u003e\u003ctable class=\"invitation-table\"\u003e\u003ctr class=\"invitation-row\" data-invite-id=\"351628621\"\u003e\u003ctd class=\"invitation-icon\"\u003e\u003cimg src=\"https://dt8kf6553cww8.cloudfront.net/static/images/icons/icon_spacer-vflN3BYt2.gif\" class=\"s_web_folder_user_32 sprite_web sprite invitation-icon\" /\u003e\u003c/td\u003e\u003ctd\u003e\u003cspan class=\"invitation-header\"\u003e\u003cspan class=\"invitation-folder-name\"\u003etest\u003c/span\u003e, invited by florian dell'aiera\u003c/span\u003e\u003cbr /\u003e\u003cspan class=\"invitation-details\"\u003e4 hrs ago\u003c/span\u003e\u003c/td\u003e\u003ctd class=\"sf-invite-button-container\"\u003e\u003cspan class=\"sf-invite-action\"\u003e\u003cspan class=\"ajax-loading-indicator\"\u003e\u003cimg src=\"https://dt8kf6553cww8.cloudfront.net/static/images/icons/ajax-loading-small-vfl3Wt7C_.gif\" /\u003e\u003c/span\u003e\u003cform action=\"/inbox_accept\" novalidate=\"novalidate\" onsubmit=\"return Sharing.register_accept(this);\" method=\"post\"\u003e\u003cinput type=\"hidden\" name=\"t\" value=\"QieCCwKi5xdK172qdmIOddoI\" /\u003e\u003cinput type=\"hidden\" name=\"_subject_uid\" value=\"64927232\" /\u003e\u003cinput type=\"hidden\" name=\"invitation\" value=\"351628621\" /\u003e\u003cinput type=\"hidden\" name=\"no_redirect\" value=\"True\" /\u003e\u003cinput type=\"submit\" class=\"freshbutton-blue\" value=\"Accept\" /\u003e\u003c/form\u003e \u003cform action=\"/inbox_decline_ajax\" novalidate=\"novalidate\" method=\"post\"\u003e\u003cinput type=\"hidden\" name=\"t\" value=\"QieCCwKi5xdK172qdmIOddoI\" /\u003e\u003cinput type=\"hidden\" name=\"_subject_uid\" value=\"64927232\" /\u003e\u003cinput type=\"hidden\" name=\"invitation\" value=\"351628621\" /\u003e\u003cinput type=\"hidden\" name=\"delete_not_on_error\" /\u003e\u003cinput type=\"button\" onclick=\"Sharing.register_decline(this, 351628621);\" value=\"Decline\" class=\"freshbutton\" /\u003e\u003c/form\u003e\u003cform novalidate=\"novalidate\" method=\"post\" class=\"view-folder-form\"\u003e\u003cinput type=\"hidden\" name=\"t\" value=\"QieCCwKi5xdK172qdmIOddoI\" /\u003e\u003cinput type=\"button\" class=\"freshbutton view-folder-button\" value=\"View folder\" /\u003e\u003c/form\u003e\u003c/span\u003e\u003c/td\u003e\u003c/tr\u003e\u003ctr class=\"invitation-row\" data-invite-id=\"351017268\"\u003e\u003ctd class=\"invitation-icon\"\u003e\u003cimg src=\"https://dt8kf6553cww8.cloudfront.net/static/images/icons/icon_spacer-vflN3BYt2.gif\" class=\"s_web_folder_user_32 sprite_web sprite invitation-icon\" /\u003e\u003c/td\u003e\u003ctd\u003e\u003cspan class=\"invitation-header\"\u003e\u003cspan class=\"invitation-folder-name\"\u003e204canards-20\u2026ll-a_f\u003c/span\u003e, invited by florian dell'aiera\u003c/span\u003e\u003cbr /\u003e\u003cspan class=\"invitation-details\"\u003e23/4/2014 10:51 AM\u003c/span\u003e\u003c/td\u003e\u003ctd class=\"sf-invite-button-container\"\u003e\u003cspan class=\"sf-invite-action\"\u003e\u003cspan class=\"ajax-loading-indicator\"\u003e\u003cimg src=\"https://dt8kf6553cww8.cloudfront.net/static/images/icons/ajax-loading-small-vfl3Wt7C_.gif\" /\u003e\u003c/span\u003e\u003cform action=\"/inbox_accept\" novalidate=\"novalidate\" onsubmit=\"return Sharing.register_accept(this);\" method=\"post\"\u003e\u003cinput type=\"hidden\" name=\"t\" value=\"QieCCwKi5xdK172qdmIOddoI\" /\u003e\u003cinput type=\"hidden\" name=\"_subject_uid\" value=\"64927232\" /\u003e\u003cinput type=\"hidden\" name=\"invitation\" value=\"351017268\" /\u003e\u003cinput type=\"hidden\" name=\"no_redirect\" value=\"True\" /\u003e\u003cinput type=\"submit\" class=\"freshbutton-blue\" value=\"Accept\" /\u003e\u003c/form\u003e \u003cform action=\"/inbox_decline_ajax\" novalidate=\"novalidate\" method=\"post\"\u003e\u003cinput type=\"hidden\" name=\"t\" value=\"QieCCwKi5xdK172qdmIOddoI\" /\u003e\u003cinput type=\"hidden\" name=\"_subject_uid\" value=\"64927232\" /\u003e\u003cinput type=\"hidden\" name=\"invitation\" value=\"351017268\" /\u003e\u003cinput type=\"hidden\" name=\"delete_not_on_error\" /\u003e\u003cinput type=\"button\" onclick=\"Sharing.register_decline(this, 351017268);\" value=\"Decline\" class=\"freshbutton\" /\u003e\u003c/form\u003e\u003cform novalidate=\"novalidate\" method=\"post\" class=\"view-folder-form\"\u003e\u003cinput type=\"hidden\" name=\"t\" value=\"QieCCwKi5xdK172qdmIOddoI\" /\u003e\u003cinput type=\"button\" class=\"freshbutton view-folder-button\" value=\"View folder\" /\u003e\u003c/form\u003e\u003c/span\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/table\u003e\u003c/div\u003e\u003cdiv class=\"db-modal-buttons\"\u003e\u003cspan class=\"ajax-loading-indicator\"\u003e\u003cimg src=\"https://dt8kf6553cww8.cloudfront.net/static/images/icons/ajax-loading-small-vfl3Wt7C_.gif\" /\u003e\u003c/span\u003e\u003cinput type=\"button\" class=\"cancel-button freshbutton-silver\" value=\"Close\" /\u003e\u003c/div\u003e\u003c/div\u003e\u003cscript\u003erequire.config({\"paths\": {}}); requirejs([], function() { // Generated by CoffeeScript 1.3.3\n\n$j('#invites-container').find('.cancel-button').on('click', function() {\n  return DBModalStack.pop();\n});\n });\u003c/script\u003e", ".dynamic-content"]]}

如果有人知道为什么它不适用于php。

感谢您的帮助

3 个答案:

答案 0 :(得分:0)

另一个尝试:三个反斜杠。

以下代码适用于我:

$output = 'div id=\"invites-container\"';

$regs = array();

$x = preg_match('/div id=\\\"invites-container\\\"/', $output, $regs);

print_r($regs);

输出:

Array
(
    [0] => div id=\"invites-container\"
)

那是方式比它需要的更难: - )

答案 1 :(得分:0)

你试过吗?

preg_match('/input type=\\\\"hidden\\\\" name=\\\\"invitation\\\\" value=\\\\"(\w+)\\\\"/', $output)

http://regex101.com/r/jE0bZ6

注意:在regex101上,它只与3个反斜杠匹配。

答案 2 :(得分:0)

没有正则表达式的另一种方式(因为你正在处理json和html):

$json = <<<'EOD'
 {"data": {}, "actions": [["html", "\u003cdiv id=\"invites-container\"\u003e\u003cdiv class=\"invitation-table-container\"\u003e\u003ctable class=\"invitation-table\"\u003e\u003ctr class=\"invitation-row\" data-invite-id=\"351628621\"\u003e\u003ctd class=\"invitation-icon\"\u003e\u003cimg src=\"https://dt8kf6553cww8.cloudfront.net/static/images/icons/icon_spacer-vflN3BYt2.gif\" class=\"s_web_folder_user_32 sprite_web sprite invitation-icon\" /\u003e\u003c/td\u003e\u003ctd\u003e\u003cspan class=\"invitation-header\"\u003e\u003cspan class=\"invitation-folder-name\"\u003etest\u003c/span\u003e, invited by florian dell'aiera\u003c/span\u003e\u003cbr /\u003e\u003cspan class=\"invitation-details\"\u003e4 hrs ago\u003c/span\u003e\u003c/td\u003e\u003ctd class=\"sf-invite-button-container\"\u003e\u003cspan class=\"sf-invite-action\"\u003e\u003cspan class=\"ajax-loading-indicator\"\u003e\u003cimg src=\"https://dt8kf6553cww8.cloudfront.net/static/images/icons/ajax-loading-small-vfl3Wt7C_.gif\" /\u003e\u003c/span\u003e\u003cform action=\"/inbox_accept\" novalidate=\"novalidate\" onsubmit=\"return Sharing.register_accept(this);\" method=\"post\"\u003e\u003cinput type=\"hidden\" name=\"t\" value=\"QieCCwKi5xdK172qdmIOddoI\" /\u003e\u003cinput type=\"hidden\" name=\"_subject_uid\" value=\"64927232\" /\u003e\u003cinput type=\"hidden\" name=\"invitation\" value=\"351628621\" /\u003e\u003cinput type=\"hidden\" name=\"no_redirect\" value=\"True\" /\u003e\u003cinput type=\"submit\" class=\"freshbutton-blue\" value=\"Accept\" /\u003e\u003c/form\u003e \u003cform action=\"/inbox_decline_ajax\" novalidate=\"novalidate\" method=\"post\"\u003e\u003cinput type=\"hidden\" name=\"t\" value=\"QieCCwKi5xdK172qdmIOddoI\" /\u003e\u003cinput type=\"hidden\" name=\"_subject_uid\" value=\"64927232\" /\u003e\u003cinput type=\"hidden\" name=\"invitation\" value=\"351628621\" /\u003e\u003cinput type=\"hidden\" name=\"delete_not_on_error\" /\u003e\u003cinput type=\"button\" onclick=\"Sharing.register_decline(this, 351628621);\" value=\"Decline\" class=\"freshbutton\" /\u003e\u003c/form\u003e\u003cform novalidate=\"novalidate\" method=\"post\" class=\"view-folder-form\"\u003e\u003cinput type=\"hidden\" name=\"t\" value=\"QieCCwKi5xdK172qdmIOddoI\" /\u003e\u003cinput type=\"button\" class=\"freshbutton view-folder-button\" value=\"View folder\" /\u003e\u003c/form\u003e\u003c/span\u003e\u003c/td\u003e\u003c/tr\u003e\u003ctr class=\"invitation-row\" data-invite-id=\"351017268\"\u003e\u003ctd class=\"invitation-icon\"\u003e\u003cimg src=\"https://dt8kf6553cww8.cloudfront.net/static/images/icons/icon_spacer-vflN3BYt2.gif\" class=\"s_web_folder_user_32 sprite_web sprite invitation-icon\" /\u003e\u003c/td\u003e\u003ctd\u003e\u003cspan class=\"invitation-header\"\u003e\u003cspan class=\"invitation-folder-name\"\u003e204canards-20\u2026ll-a_f\u003c/span\u003e, invited by florian dell'aiera\u003c/span\u003e\u003cbr /\u003e\u003cspan class=\"invitation-details\"\u003e23/4/2014 10:51 AM\u003c/span\u003e\u003c/td\u003e\u003ctd class=\"sf-invite-button-container\"\u003e\u003cspan class=\"sf-invite-action\"\u003e\u003cspan class=\"ajax-loading-indicator\"\u003e\u003cimg src=\"https://dt8kf6553cww8.cloudfront.net/static/images/icons/ajax-loading-small-vfl3Wt7C_.gif\" /\u003e\u003c/span\u003e\u003cform action=\"/inbox_accept\" novalidate=\"novalidate\" onsubmit=\"return Sharing.register_accept(this);\" method=\"post\"\u003e\u003cinput type=\"hidden\" name=\"t\" value=\"QieCCwKi5xdK172qdmIOddoI\" /\u003e\u003cinput type=\"hidden\" name=\"_subject_uid\" value=\"64927232\" /\u003e\u003cinput type=\"hidden\" name=\"invitation\" value=\"351017268\" /\u003e\u003cinput type=\"hidden\" name=\"no_redirect\" value=\"True\" /\u003e\u003cinput type=\"submit\" class=\"freshbutton-blue\" value=\"Accept\" /\u003e\u003c/form\u003e \u003cform action=\"/inbox_decline_ajax\" novalidate=\"novalidate\" method=\"post\"\u003e\u003cinput type=\"hidden\" name=\"t\" value=\"QieCCwKi5xdK172qdmIOddoI\" /\u003e\u003cinput type=\"hidden\" name=\"_subject_uid\" value=\"64927232\" /\u003e\u003cinput type=\"hidden\" name=\"invitation\" value=\"351017268\" /\u003e\u003cinput type=\"hidden\" name=\"delete_not_on_error\" /\u003e\u003cinput type=\"button\" onclick=\"Sharing.register_decline(this, 351017268);\" value=\"Decline\" class=\"freshbutton\" /\u003e\u003c/form\u003e\u003cform novalidate=\"novalidate\" method=\"post\" class=\"view-folder-form\"\u003e\u003cinput type=\"hidden\" name=\"t\" value=\"QieCCwKi5xdK172qdmIOddoI\" /\u003e\u003cinput type=\"button\" class=\"freshbutton view-folder-button\" value=\"View folder\" /\u003e\u003c/form\u003e\u003c/span\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/table\u003e\u003c/div\u003e\u003cdiv class=\"db-modal-buttons\"\u003e\u003cspan class=\"ajax-loading-indicator\"\u003e\u003cimg src=\"https://dt8kf6553cww8.cloudfront.net/static/images/icons/ajax-loading-small-vfl3Wt7C_.gif\" /\u003e\u003c/span\u003e\u003cinput type=\"button\" class=\"cancel-button freshbutton-silver\" value=\"Close\" /\u003e\u003c/div\u003e\u003c/div\u003e\u003cscript\u003erequire.config({\"paths\": {}}); requirejs([], function() { // Generated by CoffeeScript 1.3.3\n\n$j('#invites-container').find('.cancel-button').on('click', function() {\n  return DBModalStack.pop();\n});\n });\u003c/script\u003e", ".dynamic-content"]]}
EOD;

$html = json_decode($json)->actions[0][1];

$dom = new DOMDocument();
@$dom->loadHTML($html);

$inputs = $dom->getElementsByTagName('input');

foreach($inputs as $input) {
    if ($input->getAttribute('type') == 'hidden' && $input->getAttribute('name') == 'invitation')
        echo $input->getAttribute('value'). "\n";
}