2017-07-29 68 views
0

我试图用echo声明输出PCRE正则表达式的唯一匹配,它没有成功。在PHP没有返回任何内容后,我尝试删除print_r,并看到var_dump和(种类)发现该问题的变量。它的应该与匹配被提交给的git分支的名字,但是$matches变量仍然是undefined/null。`preg_match`未能得到结果

$rawPost = print_r($_POST, true); 
$pattern = '/(?<="ref":"refs\/heads\/).+?(?=")/'; 
preg_match($pattern, $rawPost, $matches); 
var_dump($matches); 

回报:

NULL 

所以,如果我理解正确的事情,正则表达式被发现没有比赛,尽管我知道这不是真的。我假设某些地方出现了语法错误,但对PHP有点新颖我不确定我做错了什么。

我传递的字符串是GitHub的网络挂接POST,如下:

Array 
(
[payload] => {"ref":"refs/heads/testing","before":"fa65fe4ec197943c7f3d88159d404ed1346259de","after":"e751fcff69075d9775640597aa893055751e587d","created":false,"deleted":false,"forced":false,"base_ref":null,"compare":"https://github.com/T99-/t99.io/compare/fa65fe4ec197...e751fcff6907","commits":[{"id":"e751fcff69075d9775640597aa893055751e587d","tree_id":"c9fc033293392e31957a52c88e9eae207ec2787b","distinct":true,"message":"Testing webhooks #3","timestamp":"2017-07-28T15:46:07-04:00","url":"https://github.com/T99-/t99.io/commit/e751fcff69075d9775640597aa893055751e587d","author":{"name":"Trevor Sears","email":"[email protected]","username":"T99-"},"committer":{"name":"Trevor Sears","email":"[email protected]","username":"T99-"},"added":[],"removed":[],"modified":["index.html"]}],"head_commit":{"id":"e751fcff69075d9775640597aa893055751e587d","tree_id":"c9fc033293392e31957a52c88e9eae207ec2787b","distinct":true,"message":"Testing webhooks #3","timestamp":"2017-07-28T15:46:07-04:00","url":"https://github.com/T99-/t99.io/commit/e751fcff69075d9775640597aa893055751e587d","author":{"name":"Trevor Sears","email":"[email protected]","username":"T99-"},"committer":{"name":"Trevor Sears","email":"[email protected]","username":"T99-"},"added":[],"removed":[],"modified":["index.html"]},"repository":{"id":95619778,"name":"t99.io","full_name":"T99-/t99.io","owner":{"name":"T99-","email":"[email protected]","login":"T99-","id":4541375,"avatar_url":"https://avatars2.githubusercontent.com/u/4541375?v=4","gravatar_id":"","url":"https://api.github.com/users/T99-","html_url":"https://github.com/T99-","followers_url":"https://api.github.com/users/T99-/followers","following_url":"https://api.github.com/users/T99-/following{/other_user}","gists_url":"https://api.github.com/users/T99-/gists{/gist_id}","starred_url":"https://api.github.com/users/T99-/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/T99-/subscriptions","organizations_url":"https://api.github.com/users/T99-/orgs","repos_url":"https://api.github.com/users/T99-/repos","events_url":"https://api.github.com/users/T99-/events{/privacy}","received_events_url":"https://api.github.com/users/T99-/received_events","type":"User","site_admin":false},"private":true,"html_url":"https://github.com/T99-/t99.io","description":"My personal website.","fork":false,"url":"https://github.com/T99-/t99.io","forks_url":"https://api.github.com/repos/T99-/t99.io/forks","keys_url":"https://api.github.com/repos/T99-/t99.io/keys{/key_id}","collaborators_url":"https://api.github.com/repos/T99-/t99.io/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/T99-/t99.io/teams","hooks_url":"https://api.github.com/repos/T99-/t99.io/hooks","issue_events_url":"https://api.github.com/repos/T99-/t99.io/issues/events{/number}","events_url":"https://api.github.com/repos/T99-/t99.io/events","assignees_url":"https://api.github.com/repos/T99-/t99.io/assignees{/user}","branches_url":"https://api.github.com/repos/T99-/t99.io/branches{/branch}","tags_url":"https://api.github.com/repos/T99-/t99.io/tags","blobs_url":"https://api.github.com/repos/T99-/t99.io/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/T99-/t99.io/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/T99-/t99.io/git/refs{/sha}","trees_url":"https://api.github.com/repos/T99-/t99.io/git/trees{/sha}","statuses_url":"https://api.github.com/repos/T99-/t99.io/statuses/{sha}","languages_url":"https://api.github.com/repos/T99-/t99.io/languages","stargazers_url":"https://api.github.com/repos/T99-/t99.io/stargazers","contributors_url":"https://api.github.com/repos/T99-/t99.io/contributors","subscribers_url":"https://api.github.com/repos/T99-/t99.io/subscribers","subscription_url":"https://api.github.com/repos/T99-/t99.io/subscription","commits_url":"https://api.github.com/repos/T99-/t99.io/commits{/sha}","git_commits_url":"https://api.github.com/repos/T99-/t99.io/git/commits{/sha}","comments_url":"https://api.github.com/repos/T99-/t99.io/comments{/number}","issue_comment_url":"https://api.github.com/repos/T99-/t99.io/issues/comments{/number}","contents_url":"https://api.github.com/repos/T99-/t99.io/contents/{+path}","compare_url":"https://api.github.com/repos/T99-/t99.io/compare/{base}...{head}","merges_url":"https://api.github.com/repos/T99-/t99.io/merges","archive_url":"https://api.github.com/repos/T99-/t99.io/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/T99-/t99.io/downloads","issues_url":"https://api.github.com/repos/T99-/t99.io/issues{/number}","pulls_url":"https://api.github.com/repos/T99-/t99.io/pulls{/number}","milestones_url":"https://api.github.com/repos/T99-/t99.io/milestones{/number}","notifications_url":"https://api.github.com/repos/T99-/t99.io/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/T99-/t99.io/labels{/name}","releases_url":"https://api.github.com/repos/T99-/t99.io/releases{/id}","deployments_url":"https://api.github.com/repos/T99-/t99.io/deployments","created_at":1498616354,"updated_at":"2017-07-12T02:03:49Z","pushed_at":1501271199,"git_url":"git://github.com/T99-/t99.io.git","ssh_url":"[email protected]:T99-/t99.io.git","clone_url":"https://github.com/T99-/t99.io.git","svn_url":"https://github.com/T99-/t99.io","homepage":null,"size":5,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"production","stargazers":0,"master_branch":"production"},"pusher":{"name":"T99-","email":"[email protected]"},"sender":{"login":"T99-","id":4541375,"avatar_url":"https://avatars2.githubusercontent.com/u/4541375?v=4","gravatar_id":"","url":"https://api.github.com/users/T99-","html_url":"https://github.com/T99-","followers_url":"https://api.github.com/users/T99-/followers","following_url":"https://api.github.com/users/T99-/following{/other_user}","gists_url":"https://api.github.com/users/T99-/gists{/gist_id}","starred_url":"https://api.github.com/users/T99-/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/T99-/subscriptions","organizations_url":"https://api.github.com/users/T99-/orgs","repos_url":"https://api.github.com/users/T99-/repos","events_url":"https://api.github.com/users/T99-/events{/privacy}","received_events_url":"https://api.github.com/users/T99-/received_events","type":"User","site_admin":false}} 
) 

我在这里测试的正则表达式:regex101 test

有没有人有任何见解我做错了什么?

+1

$ rawPost是一个数组吗?您无法将数组传递给preg_match。 – Chase

+0

@Chase $ rawPost是从'print_r()'返回的字符串。 – Barmar

+0

为什么你使用'print_r'和一个正则表达式?为什么不只是'json_decode($ _ POST ['payload'])'? – Barmar

回答

1

也许你对这个有点太过分了。为什么不解码嵌入在有效载荷中的JSON?

$payload = json_decode($_POST['payload'],true); 
$ref = $payload['ref']; 

var_dump($ref);