iLLeLogicaL Posted June 2, 2007 Share Posted June 2, 2007 Wondering how to grab pieces of text from a file. Let's say I opened a webpage and want to have all the values of the input fields. This would be the contents of the file <html> <head> ... </head> <body> ... <input type='hidden' name='hidden_w' value='123456' /> ... </body> </html> Now I want that value of the input field 'hidden_w' to be in the variable $w. How do I this? (w. explanation please) -- iLLeLogicaL -- Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 2, 2007 Share Posted June 2, 2007 couldnt you have $w = $_POST['hidden_w']; Quote Link to comment Share on other sites More sharing options...
iLLeLogicaL Posted June 2, 2007 Author Share Posted June 2, 2007 No, because i'm reading from an external website and I can't grab the input unless I hack the server and change the action file with one of my own Quote Link to comment Share on other sites More sharing options...
taith Posted June 2, 2007 Share Posted June 2, 2007 well... gotta start somewhere... $file=file_get_contents('url.html'); $inputs=strip_tags($file,'<input>'); that'd give you all the input tags... then you gotta filter them somehow... prolly regex... Quote Link to comment Share on other sites More sharing options...
iLLeLogicaL Posted June 2, 2007 Author Share Posted June 2, 2007 So that will give me a file that will only contain the input fields ? The meaning escapes me Quote Link to comment Share on other sites More sharing options...
taith Posted June 2, 2007 Share Posted June 2, 2007 sorry... by file i mean string... $file=file_get_contents('url.html'); $inputs=strip_tags($file,'<input>'); echo $inputs; #may contain '<input type=textbox name="" value="">asdfasdf<input type=submit>there could be a paragraph or two in here...<input type=file name="">asdfadf' Quote Link to comment Share on other sites More sharing options...
taith Posted June 2, 2007 Share Posted June 2, 2007 i jus made this... you can scrap the text outta that string too with this... <?php function strip_text($string){ $tag=false; $strlen=strlen($string); for($i=0; $i<$strlen; $i++){ if($string{$i}=='<') $tag=true; elseif($string{$i}=='>'){ $out.=$string{$i}; $tag=false; } if($tag==true) $out.=$string{$i}; } return $out; } ?> so.... $file=file_get_contents('url.html'); $inputs=strip_tags(strip_text($file),'<input>'); echo $inputs; #may contain '<input type=textbox name="" value=""><input type=submit><input type=file name="">' Quote Link to comment Share on other sites More sharing options...
iLLeLogicaL Posted June 3, 2007 Author Share Posted June 3, 2007 Ok works nice, but still can you make it that it takes the names in arrays & the values in other arrays? Or explain it a bit ? Quote Link to comment Share on other sites More sharing options...
sasa Posted June 3, 2007 Share Posted June 3, 2007 try <?php $text ="<html> <head> ... </head> <body> ... <input type='hidden' name='hidden_w' value='123456' /> ... <input type='hidden' name='hidden_x' value='654321' /> <input type='hidden' name='y' value='654321' /> </body> </html>"; $out = array(); $text = str_replace("'",'"',$text); //find hidden inputs preg_match_all ('/<input[^>]+hidden[^>]+>/',$text,$b); $x = $b[0]; foreach ($x as $a){ //find name properties preg_match('/name="([^"]+)"/',$a,$c); $key = $c[1]; $key = str_replace('hidden_','', $key); //find value preg_match('/value="([^"]+)"/',$a,$c); $value = $c[1]; $out[$key] = $value; } print_r($out); ?> Quote Link to comment Share on other sites More sharing options...
iLLeLogicaL Posted June 5, 2007 Author Share Posted June 5, 2007 Works fine till it encounters an empty value field Can you re-edit again so it does work then to, because I don't get your code :-X Quote Link to comment Share on other sites More sharing options...
sasa Posted June 5, 2007 Share Posted June 5, 2007 OK change last line to if($value) $out[$key] = $value; Quote Link to comment Share on other sites More sharing options...
iLLeLogicaL Posted June 5, 2007 Author Share Posted June 5, 2007 Nah that just makes it worse Ok let's say this. I know that when I load the page there are always 5 fields, 3 who're hidden, and 3 who's name change everyday (wonder why? ) But I know that the last 3 are always empty (as is the first one). But it only shows me the name of the 3 first fields, then stops after the 3 field (no value's in array but still 2 fields are after that <input type="hidden" name="w" value=""> <input type="hidden" name="login" value="999999999"><!-- CHANGING VALUE --> <p><table class="p1" style="width:100%"cellspacing="1" cellpadding="0"><tr><td> <table width="100%" cellspacing="1" cellpadding="0"> <tr><td><label>Naam:</label> <input class="fm fm110" type="text" name="e9b0679" value="" maxlength="15"> <span class="e f7"></span><!-- CHANGING NAME --> </td></tr> <tr><td><label>Wachtwoord:</label> <input class="fm fm110" type="password" name="e5cd2eb" value="" maxlength="20"> <span class="e f7"></span><!-- CHANGING NAME --> </td></tr> </table></td></tr></table></p> <p align="center"><input type="hidden" name="ec75e7c" value=""><!-- CHANGING NAME --> Now I want the names contained in array dunnow named $names or so, and the values in a container name $values with a cifer as index I may be asking to much, but we all need to learn don't we ? Quote Link to comment Share on other sites More sharing options...
sasa Posted June 5, 2007 Share Posted June 5, 2007 if you wont to grab all type of input, not just hidden, change line preg_match_all ('/<input[^>]+hidden[^>]+>/',$text,$b); to preg_match_all ('/<input[^>]+>/',$text,$b); Quote Link to comment Share on other sites More sharing options...
iLLeLogicaL Posted June 6, 2007 Author Share Posted June 6, 2007 Thank you very much Sasa, It works perfectly now 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.