`preg_match`无法获得结果

时间:2017-07-29 04:07:00

标签: php regex pcre

我正在尝试输出PCRE正则表达式与echo语句的唯一匹配,但尚未成功。在PHP没有返回任何内容之后,我尝试删除print_r并看到变量var_dump和(种类)发现了问题。 应该匹配已提交的git分支的名称,但$matches变量仍为undefined / null。

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

返回:

NULL

因此,如果我正确理解事情,正则表达式找不到匹配,即使我知道这不是真的。我假设某些地方在语法上是错误的,但对PHP来说有点新,我不确定我做错了什么。

我传入的字符串是GitHub webhook 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":"trevorsears.main@gmail.com","username":"T99-"},"committer":{"name":"Trevor Sears","email":"trevorsears.main@gmail.com","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":"trevorsears.main@gmail.com","username":"T99-"},"committer":{"name":"Trevor Sears","email":"trevorsears.main@gmail.com","username":"T99-"},"added":[],"removed":[],"modified":["index.html"]},"repository":{"id":95619778,"name":"t99.io","full_name":"T99-/t99.io","owner":{"name":"T99-","email":"trevorsears.main@gmail.com","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":"git@github.com: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":"trevorsears.main@gmail.com"},"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 个答案:

答案 0 :(得分:1)

也许你过分夸大了这一点。为什么不解码有效负载中嵌入的JSON?

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

var_dump($ref);