2017-07-29 5 views
0

を結果取得に失敗しました。 PHPは何も返されなかった後、私は問題を発見print_rを排除し、var_dumpと(種類の)で変数を見てみました。それは試合になっにコミットされたGitのブランチの名前だが、代わりに$matches変数はNULL /未定義のまま。`preg_match`は、私は成功していないecho文と出力にPCRE正規表現の唯一の試合をしようとしている

$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

はおそらく、あなたはこの少し多すぎるをoverthinkingしています。なぜペイロードに埋め込まれたJSONをデコードしないのですか?

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

var_dump($ref); 
関連する問題