2016-04-28 81 views
-2

提取字符串我有這樣的一個字符串,我從API獲取請求得到的:PHP:從字符串

HTTP/1.1 404 Not Foundset-cookie: test-usercontext=asdf-client=200; path= 
/set-cookie: test=AjQxMDMBABhNAEEATgBHAE8AIAAgACAAIAAgACAAIAACAAYyADAAMAADABBGAEIAVAAgACAAIAAgACAABAAYMgAwADEANgAwADQAMgA4ADEAMAAzADQABQAEAAAACAYAAlgACQACRQD 
%2fA%2fowggP2BgkqhkiG9w0BBwKgggPnMIID4wIBATELMAkGBSsOAwIaBQAwCwYJKoZIhvcNAQcBoIICwjCCAr4wggJ%21AgcgE 
wUWFQk1MAkGByqGSM44BAMwRTELMAkGA1UEBhMCREUxDzANBgNVBAoTBlNBUC1BRzEMMAoGA1UECxMDRkJUMRcwFQYDVQQDDA4qL 
ndkZi5zYXAuY29ycDAeFw0xMzA1MTYxNTA5MzVaFw0zODAxMDEwMDAwMDFaMEUxCzAJBgNVBAYTAkRFMQ8wDQYDVQQKEwZTQVAtQUcxDDAKBgNVBAsTA0ZCVDEXMBUGA1UEAwwOKi53ZGYuc2FwLmNvcnAwggG4MIIBLAYHKoZIzjgEATCCAR8CgYEA 
%2f%21RvacHcYrHXGs0tkbb0V5pRZr%2166wpEefngcrA%2feo5VUyPFnjV%21VLPgYTlK5oVBT%21b8hVdVDF72%2fF7tG%2fzyNXjr6xCkACw69 
%21NEBKQ4A7RSfLieN8IgCWl3ENA6cbgQJlhYMAugQexgN5rNX67eLyIFTEHGbF%2fkvlWXyDZt2AsCFQCCZiO7z37X8dHkeMyzasFRThlyFwKBgQC6 
%2f4ZVeZJsMH528dpoLej8jEeUYstPpe6CFgIF7LqFjFnEWM%2fgt364k9%21kYRMdnN1l3Dgv2fHBOMET2lwiCSP%21Lp3JkDH9K 
%21H9zLJfg%2fd3o5gNCu4pct9Au4%2fLh%21ejlgOAytMcr7z9G1SSaRKFk9QnzfyA4mmLmtDT6qZG%2fgx5%21wOBhQACgYEA%21rGBozVMIrQFy8mXlkOSDWiAo16gBBlLqAIhsc3Th 
%2f6LFM%21HK8mzuxApdB8vDg5zOPQW959g2YmZBvMm7fBjjRyeQnh0SamBQNwdWR3cyhdXANRJTjdjV6yGJaBVUzMam5Yh2%2fNjwHWxS 
%21Ev%21KNSUi7%2fHLrYjQ%2f5mYiipmBwOskwCQYHKoZIzjgEAwMvADAsAhQkPKrX2UIzsWHZoErU9zMKRjRMSwIUchu3vWrhXc2bckO 
%21ASRCiCh3oF4xgf0wgfoCAQEwUDBFMQswCQYDVQQGEwJERTEPMA0GA1UEChMGU0FQLUFHMQwwCgYDVQQLEwNGQlQxFzAVBgNVB 
AMMDioud2RmLnNhcC5jb3JwAgcgEwUWFQk1MAkGBSsOAwIaBQCgXTAYBgkqhkiG9w0BCQMxCwYJKoZIhvcNAQcBMBwGCSqGSIb3D 
QEJBTEPFw0xNjA0MjgxMDM0NTdaMCMGCSqGSIb3DQEJBDEWBBTLTQarxV7dyga8cQOmMUBQNBc4mTAJBgcqhkjOOAQDBC4wLAIUUOp 
%21DiZu22HaQ324gaZTLbbjWqICFCtwUwws0te2HqBON0OF%2faQ%21hLD3; path=/; domain=test.de; secure; HttpOnlyset-cookie 
: asdf_SESSIONID=VMoFGADsOTEThFHqCdDaC9mG0O4NLBHmhakAUFaDZVE%3d; path=/; secure; HttpOnlycontent-type 
: application/jsoncontent-length: 504x-csrf-token: dZgtpkwUMaN-gQ1X4QEXUw==dataserviceversion: 1.0x-asdf-e2eperf 
: { asName: "ldai1_00", asType: "asdf", elapsed: 319, maxMem: 4595, dbTime: 166 }{"error":{"code" 
:"005056A509B11EE1B9A8FEC11C21D78E","message":{"lang":"en","value":"Resource not found for the segment 
'http:'."},"innererror":{"transactionid":"5721AEEC42F83183E10000000A4DA014","timestamp":"20160428103457 
.6579540","Error_Resolution":{"error","Note":"Note 1797736 
for error analysis"}}}}<br> 

我想提取的X-CSRF令牌,這是上市後:x-csrf-token:

在這種情況下,將

dZgtpkwUMaN-gQ1X4QEXUw ==

如何做到這一點的最好方法是什麼?

我使用preg_match_all嗎?但是,那麼,我有點失落。 謝謝你的任何建議。

+1

爲什麼正則表達式只是執行字符串搜索 – anubhava

回答

2

此正則表達式將工作

x-csrf-token:(.*?)(?=\w+:) 

Regex Demo

PHP代碼

$re = "/x-csrf-token:(.*?)(?=\\w+:)/m"; 
preg_match_all($re, $str, $matches); 
print_r($matches[1]); 

Ideone Demo

一個更好的解決辦法是停止的時候有空間除了\w+:(假設空間不能是x-csrf-token一部分)

x-csrf-token:\s*(.*?)(?=\w+:|\s) 
0

您可以使用csrf-token: (.*?==)preg_match_all,即:

preg_match_all('/csrf-token: (.*?==)/im', $string, $token, PREG_PATTERN_ORDER); 
$token = $token[1][0]; 
echo $token; 
//dZgtpkwUMaN-gQ1X4QEXUw== 

Ideone Demo