tryingtolearn Posted July 26, 2008 Share Posted July 26, 2008 Im using file_get_contents to get a string from a form input The result is like this "250","134","image1.gif",""," Joe - hosted ","http://www.url.com/?l65j", "250","134","image2.gif",""," Sally - hosted ","http://www.url.com/?Ut59", "250","134","image3.gif",""," Mary - Not hosted ","http://www.url.com/?MN7F", "250","134","image4.gif",""," Joe - hosted ","http://www.url.com/?WrT3", Is it possible to break down the string for each url and perform an action on each one? I tried various things with strstr and substr but it only returns the first url. So I thought I needed to get each one in an array but I cant seem to get that to work Any ideas?? Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 26, 2008 Share Posted July 26, 2008 you could break apart all the lines using explode("\n",$file) the loop through them all using a foreach Scott. Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted July 26, 2008 Author Share Posted July 26, 2008 I was trying that also but I get an invalid argument supplied for foreach() This is what the form action looks ;ike if(isset($_POST['input'])) { $source = file_get_contents($_POST['input']); $output = htmlspecialchars($source); explode("\n",$output); foreach($output as $key => $value){ echo"The value at $key is $value<br />"; } } Quote Link to comment Share on other sites More sharing options...
MFHJoe Posted July 26, 2008 Share Posted July 26, 2008 $output needs to change to the exploded value: if(isset($_POST['input'])) { $source = file_get_contents($_POST['input']); $output = htmlspecialchars($source); $output = explode("\n",$output); foreach($output as $key => $value){ echo"The value at $key is $value<br />"; } } Should work, as long as the $_POST['input'] actually has newlines in it. Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted July 26, 2008 Author Share Posted July 26, 2008 That worked but the results arent what I was looking for That took each line of the source code of the page and added it to the array ex The value at 0 is <html> The value at 1 is <head> etc.... So I guess I should add $source is going to be something like this <html> <head> <title>Untitled</title> </head> <body> "250","134","image1.gif",""," Joe - hosted ","http://www.url.com/?l65j", "250","134","image2.gif",""," Sally - hosted ","http://www.url.com/?Ut59", "250","134","image3.gif",""," Mary - Not hosted ","http://www.url.com/?MN7F", "250","134","image4.gif",""," Joe - hosted ","http://www.url.com/?WrT3", </body> </html> And I need to extract and perfom an action on each url within I guess that bit of onfo would have helped - sorry Quote Link to comment Share on other sites More sharing options...
MFHJoe Posted July 26, 2008 Share Posted July 26, 2008 You could add the strip_tags function into the mix. But that would leave Untitled at the top... if(isset($_POST['input'])) { $source = file_get_contents($_POST['input']); $output = strip_tags($source); $output = explode("\n",$output); foreach($output as $key => $value){ echo"The value at $key is $value<br />"; } } There may be another way of doing it that gets rid of Untitled as well. But it probably won't be as simple. Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted July 26, 2008 Author Share Posted July 26, 2008 Im thinking I might be going about this wrong all together I dont know, basically I need to open $_POST['input'] (Which will be a url from a form) search the source code of that (Which is why I was using file_get_contents) and perfom an action each time it finds http://www.url.com/?l65j But everything after the ? is different for each one Quote Link to comment Share on other sites More sharing options...
MFHJoe Posted July 26, 2008 Share Posted July 26, 2008 I wrote how I would handle something like this below. I've tried to comment it so you understand how I've changed it. Basically, using the code below, you can access that URL (http://www.url.com/?l65j) with the variable $splitline[5]. You can also get other things from those lines (like the image filename) using $splitline[2]. Run the code below, you'll see the rest. <?php // Get contents of html file $source = file_get_contents('go.html'); // Strip the HTML out $output = strip_tags($source); // Put every new line into an array $output = explode("\n",$output); // Get rid of the blank array entries $output = array_filter($output); // For each array entry remaining: foreach($output as $value) { // Split each line into the different sections (separated by commas) $splitline = explode(',', $value); // If the line has 5 values (this just gets rid of the line saying 'Untitled' which we don't want) if(isset($splitline[5])) { // Echo the values of the line echo 'Number 1: ' . $splitline[0] . '<br />'; echo 'Number 2: ' . $splitline[1] . '<br />'; echo 'Image Filename: ' . $splitline[2] . '<br />'; echo '[nothing] ' . $splitline[3] . '<br />'; echo 'Person who hosted the file?: ' . $splitline[4] . '<br />'; echo 'Url: ' . $splitline[5] . '<br />'; echo '<hr />'; } } ?> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 26, 2008 Share Posted July 26, 2008 I threw one together with Regular Expressions. if(isset($_POST['input'])) { $file = file_get_contents($_POST['input']); //Lets get just the stuff between the two body tags $source = preg_replace("/\<body>(.*?)\<\/body>/","\\1",$file); //Grab the matches while(preg_match("/http:\/\/www.url.com\/\?([a-zA-Z0-9]+)/",$source,$tmp_matches)){ $matches[] = $tmp_matches[1]; $source = preg_replace("/http:\/\/www.url.com\/\?([a-zA-Z0-9]+)/","",$source,1); } foreach($matches as $action){ //now you can use the $action variable, which is the number after the ? in each of the links. echo "Action Number: {$action}<br />"; } } Of course you'll want to change it from www.url.com no doubt. If you have trouble doing that just let me know. Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted July 26, 2008 Author Share Posted July 26, 2008 MFHJoe That is really great! I will work with that a bit Thank you very much Project Fear I got a Unknown modifier '/' error for this line while(preg_match("/http:\/\/www.url.com\/\?([a-zA-Z0-9]+)/",$source,$tmp_matches)){ Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 26, 2008 Share Posted July 26, 2008 Oh dang. Don't worry about it. Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted July 27, 2008 Author Share Posted July 27, 2008 MFHJoe Thanks again, Played around with what you posted and got it working just like I needed. I appreciate you taking the time to help me. 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.