jamesxg1 Posted May 14, 2010 Share Posted May 14, 2010 Hiya peeps! If I was to have something like this. {POST = test} {GET = testtwo} <html> <title>This is a test</title> <body> Test Body. </body> </html> How do I get {POST = test} & {GET = testtwo} (they could be anything not just POST and GET) and store them in an array EG. array('POST' => 'test', 'GET' => 'testtwo'); then remove them from the html file and echo it. Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/201812-how-do-i-do-a-match-set-and-remove/ Share on other sites More sharing options...
kenrbnsn Posted May 15, 2010 Share Posted May 15, 2010 Please explain what you are trying to do better. Ken Quote Link to comment https://forums.phpfreaks.com/topic/201812-how-do-i-do-a-match-set-and-remove/#findComment-1058604 Share on other sites More sharing options...
jamesxg1 Posted May 15, 2010 Author Share Posted May 15, 2010 Hiya Ken, Thanks for the reply, basically Im trying to do this. Result before filtered. {POST = test} {GET = testtwo} <html> <title>This is a test</title> <body> Test Body. </body> </html> Result after filtered. <html> <title>This is a test</title> <body> Test Body. </body> </html> array('POST' => 'test', 'GET' => 'testtwo'); So basically I'm trying to search from {POST = test} & {GET = testtwo} in the html file that is stored in a var EG. $html = '{POST = test} {GET = testtwo} <html> <title>This is a test</title> <body> Test Body. </body> </html>'; And turn the values that are in brackets into and array. The values may vary so it might not be POST or GET. Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/201812-how-do-i-do-a-match-set-and-remove/#findComment-1058646 Share on other sites More sharing options...
jamesxg1 Posted May 15, 2010 Author Share Posted May 15, 2010 BUMP Quote Link to comment https://forums.phpfreaks.com/topic/201812-how-do-i-do-a-match-set-and-remove/#findComment-1058657 Share on other sites More sharing options...
jamesxg1 Posted May 15, 2010 Author Share Posted May 15, 2010 Anyone have any ideas :S ? Quote Link to comment https://forums.phpfreaks.com/topic/201812-how-do-i-do-a-match-set-and-remove/#findComment-1058689 Share on other sites More sharing options...
jamesxg1 Posted May 16, 2010 Author Share Posted May 16, 2010 BUMP Quote Link to comment https://forums.phpfreaks.com/topic/201812-how-do-i-do-a-match-set-and-remove/#findComment-1059200 Share on other sites More sharing options...
DavidAM Posted May 16, 2010 Share Posted May 16, 2010 You are going to need to use preg_match_all() to do this; at least, that is probably the cleanest solution. I am not very good with regular expressions, but I think it would be something like this: $html = '{POST = test} {GET = testtwo} <html> <title>This is a test</title> <body> Test Body. </body> </html>'; $regEx = '/\{(.*?) ?= ?(.*?)\}/'; $found = array(); $data = array(); if (preg_match_all($regEx, $html, $found, PREG_SET_ORDER)) { // Found at least one foreach ($found as $entry) { $data[$entry[1]] = $entry[2]; } } // now remove the stuff $html = preg_replace($regEx, '', $html); Someone in the PHP RegEx Section may be able to give you a better (correct?) regular expression; as I said, I'm not very good with it. Disclaimer: This code is untested, and may be completely wrong. Quote Link to comment https://forums.phpfreaks.com/topic/201812-how-do-i-do-a-match-set-and-remove/#findComment-1059216 Share on other sites More sharing options...
Jiin Posted May 16, 2010 Share Posted May 16, 2010 Maybe you could echo the $html to a file and then: $fh = fopen("myfile.txt", "r"); while(i=0,i<2,i++) { $line = fgets($fh); array=('1stpart of line one' => 'second part of line one', '1stpart of line two' => 'second part of line two'); echo $array[0]; echo $array[1]; } Quote Link to comment https://forums.phpfreaks.com/topic/201812-how-do-i-do-a-match-set-and-remove/#findComment-1059221 Share on other sites More sharing options...
jamesxg1 Posted May 17, 2010 Author Share Posted May 17, 2010 You are going to need to use preg_match_all() to do this; at least, that is probably the cleanest solution. I am not very good with regular expressions, but I think it would be something like this: $html = '{POST = test} {GET = testtwo} <html> <title>This is a test</title> <body> Test Body. </body> </html>'; $regEx = '/\{(.*?) ?= ?(.*?)\}/'; $found = array(); $data = array(); if (preg_match_all($regEx, $html, $found, PREG_SET_ORDER)) { // Found at least one foreach ($found as $entry) { $data[$entry[1]] = $entry[2]; } } // now remove the stuff $html = preg_replace($regEx, '', $html); Someone in the PHP RegEx Section may be able to give you a better (correct?) regular expression; as I said, I'm not very good with it. Disclaimer: This code is untested, and may be completely wrong. That worked amazingly! Thank you very very much! Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/201812-how-do-i-do-a-match-set-and-remove/#findComment-1059493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.