spfoonnewb Posted January 21, 2007 Share Posted January 21, 2007 Hi,I am in need of a code that can search a text file for a $_GET valueSo say the file has www.domain.com/index.php?get=thevalueI want php to search for the GET (get), and return the value..(thevalue) any help would be appreciated. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 21, 2007 Share Posted January 21, 2007 you could use regular expressions, or you could use str_pos to find get, then just go to the end of the line using substr. Regexp might be the way to go. Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted January 21, 2007 Author Share Posted January 21, 2007 Ok, heres what I have.. although it does not work ... I do need help with the strpos thing, to find the value. Thanks.The current error is:Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslashStrpos just returns a number.[code]<form action="" method="POST"><input type="text" name="url"><input type="submit" value="Submit"></form><?php$location = $_POST["url"];$ch = curl_init("http://$location");$fp = fopen("test.txt", "w");curl_setopt($ch, CURLOPT_FILE, $fp);curl_setopt($ch, CURLOPT_HEADER, 0);curl_exec($ch);curl_close($ch);fclose($fp);$filename = "test.txt";$handle = fopen($filename, "r");$read = fread($handle, filesize($filename));$newstring = preg_match('get', $read);echo "$newstring";fclose($handle);?>[/code] Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 21, 2007 Share Posted January 21, 2007 Your regular expressions have to match expressions$newstring = preg_match('get', $read);The pregmatch, you can't tell it to just find a word, pregmatch is for finding words that match a regular expression.something likepreg_match("/$_GET[/i", "pathtofile/page.php") However, I am confused about this, because if you do $_GET['something'] it will literally look for something, so try and just check for $_GET[ then you know you have a match. Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted January 21, 2007 Share Posted January 21, 2007 You need ot define a delimiter for your regex within the preg_replace function. Traditionally /'s are used.[code]<?php$newstring = preg_match('/get/', $read);?>[/code]Once you identify the urls in the text file you may find the following code useful:[code]<?php$url = 'www.domain.com/index.php?foo=1&bar=2&baz=3';print_r(getVars($url));function getVars($url){ $tmp = explode('?', $url); $str = $tmp[1]; $ret = array(); parse_str($str, $ret); return $ret;}// Output: Array ( [foo] => 1 [bar] => 2 [baz] => 3 )?>[/code]Best,Patrick Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 21, 2007 Share Posted January 21, 2007 [quote]Once you identify the urls in the text file you may find the following code useful:[/quote]Yes, that was a very nice function, I can even use that as an example later, nice work. Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted January 21, 2007 Author Share Posted January 21, 2007 Hi, thanks for your time. I am still having trouble identifying the URL through the regular expressions/ preg_match. Trying through your example only returns a number "1".Basically it is searching a text file, full of HTML code, for a string, or in this example "get".Once it finds get, I need it to return the value of get. So say: [code]<html><head><title>Test</title></head><body><p>Body Content</p><p><a href="http://www.domain.com/index.php?get=test">The link</a></p></body></html>[/code]I need php to return "test", this because get=test in the link. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 21, 2007 Share Posted January 21, 2007 $newstring = preg_match('/get=[a-z]*\\"/', $read);??I am not the best at regular expressions yet, far from it.Maybe that will work. That should check for get=, then whatever might be beside it, until the ".Probably way off, but wanted to try. Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted January 21, 2007 Share Posted January 21, 2007 Are you looking for all links and all arbitrary query strings or will the urls you are interested in always have a GET variable named get? Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted January 21, 2007 Author Share Posted January 21, 2007 $_GET will always equal "get" I just need the value from it, I only want the first one it comes across to be echoed. Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted January 21, 2007 Author Share Posted January 21, 2007 I finally got one to work, but how would I get it to display the value of get, instead of "0"? (0 meaning that it matched it) Quote Link to comment 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.