Jump to content

[SOLVED] string data help needed


tryingtolearn

Recommended Posts

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??

Link to comment
Share on other sites

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 />";
}	

}

 

 

 

 

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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 />';
}
}	
?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.