Jump to content

Recommended Posts

If i had a set of numbers on an html page: 1,2,3 4,5,6 7,8,9

 

How would I be able to get php to retrieve the page, and format the values into $variables?

 

e.g.

$set1-1 = 1
$set1-2 = 2
$set1-3 = 3

$set2-1 = 4
$set2-2 = 5
$set2-3 = 6

$set3-1 = 7
$set3-2 = 8
$set3-3 = 9

 

I would then save the variables into a database, but I know how to do that.

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/
Share on other sites

Please look at file_get_contents function in the php manual for getting the file info.

 

As for the variables:

<?php
$string = "1,2,3 4,5,6 7,8,9";
list($first, $second, $third) = explode(" ", $string);
list($set11, $set12, $set13) = explode(",", $first);
list($set21, $set22, $set23) = explode(",", $second);
list($set31, $set32, $set33) = explode(",", $third);
?>

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/#findComment-553937
Share on other sites

ok, so that is first splitting the file into first, second, etc using the space. Then its taking the split variables and splitting them into sub variables using commas?

 

This works.

$file = file_get_contents ('http://website.com/stringpage.html');
echo $file;

 

At the end of the file there is junk data I dont want to put into variables, do I just stop the $tenth etc variable exploding before it gets to them?

 

The junk appears to be at the end of each http page

 

-1,-1 -1,-1 -1,-1 -1,-1

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/#findComment-553941
Share on other sites

Or you could set a limit on the explode.

<?php
$string = "1,2,3 4,5,6 7,8,9";
list($first, $second, $third, $junk) = explode(" ", $string, 4);
list($set11, $set12, $set13) = explode(",", $first);
list($set21, $set22, $set23) = explode(",", $second);
list($set31, $set32, $set33) = explode(",", $third);
?>

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/#findComment-553946
Share on other sites

this is the whole contents of the page:

 

2855,2120,197517875 3825,99,21367817 3177,99,18912900 1529,99,23891398 3382,99,24350662 18207,96,10122224 1885,99,13149819 56936,89,4998462 3694,99,15041475 229226,80,2019245 70576,91,6006279 61428,83,2785348 3065,99,13229364 24683,80,1990823 39221,74,1102697 92920,75,1284583 9061,82,2438605 5669,81,2287788 13137,83,2677386 6202,91,5924583 10378,82,2455028 19154,73,1019663 19710,79,1928393 89,99,13472515 432,89,5060818 2154,1943 -1,-1 -1,-1 -1,-1

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/#findComment-553966
Share on other sites

well here's my take:

 

<?php
// dummy var holding the data
$content = "2855,2120,197517875 3825,99,21367817 3177,99,18912900 1529,99,23891398 3382,99,24350662 18207,96,10122224 1885,99,13149819 56936,89,4998462 3694,99,15041475 229226,80,2019245 70576,91,6006279 61428,83,2785348 3065,99,13229364 24683,80,1990823 39221,74,1102697 92920,75,1284583 9061,82,2438605 5669,81,2287788 13137,83,2677386 6202,91,5924583 10378,82,2455028 19154,73,1019663 19710,79,1928393 89,99,13472515 432,89,5060818 2154,1943 -1,-1 -1,-1 -1,-1";

$a = explode(',',$content); // explode each comma separated set into an array

// for each position in the array...
foreach ($a as $b) { 
   $c[] = explode(' ',$b); // explode each space separated set into a nested array, making a 2d array overall
}		

// if you wanted to echo say, that 4th number 3825
// that would be the 2nd number in the 3rd set. 
// remember, positions start at zero...
echo $c[2][1];

// ..or, just to display it all
foreach($c as $key => $val) {
   $x = 0;
   while ($val[$x]) {
      echo "$key - $x => " . $val[$x] . "<br>";
      $x++;
   }
   echo "<br>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/#findComment-553977
Share on other sites

oops it seems you said each "set" is separated by spaces not commas.  All you have to do is move the comma in the first explode to the 2nd explode

$a = explode(' ',$content); // explode each comma separated set into an array

// for each position in the array...
foreach ($a as $b) { 
   $c[] = explode(',',$b); // explode each space separated set into a nested array, making a 2d array overall
}

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/#findComment-553997
Share on other sites

Hmm, its not seperating them properly still

<?PHP
$a = explode(' ',$file); // explode each space separated set into an array
// for each position in the array...
foreach ($a as $b) { 
   $c[] = explode(',',$b); // explode each comma separated set into a nested array, making a 2d array overall
}

echo $c[0][0].' ';
echo $c[0][1].' ';
echo $c[0][2];
?>

 

gives:

 

2862 2120 197673962 3811

 

It shouldnt be getting the last number?

 

2862,2120,197673962 3811,99,21402359 3154,99,18951751 1521,99,23933230 3363,99,24389057 18236,96,10122224 1886,99,13149819 56986,89,4998721 3699,99,15041475 229424,80,2019245 70640,91,6006279 61490,83,2785348 3077,99,13229364 24708,80,1990823 39247,74,1102797 92965,75,1284583 9073,82,2438640 5679,81,2287834 13149,83,2677386 6215,91,5926315 10391,82,2455028 19167,73,1019663 19752,79,1928393 89,99,13472515 439,89,5061113 2156,1943 -1,-1 -1,-1 -1,-1 

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/#findComment-554356
Share on other sites

This is all the code:

 

$file = file_get_contents ('http://link.html');
echo "<p></p>";

$a = explode(' ',$file); // explode each space separated set into an array
// for each position in the array...
foreach ($a as $b) { 
   $c[] = explode(',',$b); // explode each comma separated set into a nested array, making a 2d array overall
}

// if you wanted to echo say, that 4th number 3825
// that would be the 2nd number in the 3rd set. 
// remember, positions start at zero...
echo $c[0][0].' ';
echo $c[0][1].' ';
echo $c[0][2];

 

Link.html

2862,2120,197673962 3811,99,21402359 3154,99,18951751 1521,99,23933230 3363,99,24389057 18236,96,10122224 1886,99,13149819 56986,89,4998721 3699,99,15041475 229424,80,2019245 70640,91,6006279 61490,83,2785348 3077,99,13229364 24708,80,1990823 39247,74,1102797 92965,75,1284583 9073,82,2438640 5679,81,2287834 13149,83,2677386 6215,91,5926315 10391,82,2455028 19167,73,1019663 19752,79,1928393 89,99,13472515 439,89,5061113 2156,1943 -1,-1 -1,-1 -1,-1 

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/#findComment-554383
Share on other sites

umm okay well i just copy/pasted and tested exactly what you posted, except for I changed $file = ... to just a string of the numbers and it outputs just the 3 vars as expected, so...my guess is that there's something different about link.html can you post the code for link.html

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/#findComment-554390
Share on other sites

okay...i'm stumped.  doing strcmp(); on the get_contents vs. a straight string from the output returns -1 meaing the get_contents string is shorter.  But when I echo them side by side, it displays the exact same thing.  But if I view source from output, get_contents string is formatted different.  Each set is on its own line, but there's no line break tags or anything (it's all strung together on the output string source). 

 

My next guess is that whatever editor you used to create the html file is using some sort of hidden formatting code or something...but at this point in time I'm just guessing.  Or maybe there's a setting on the server that does that. I really don't know.  You gave me a link to the page..do you have the actual source file or know what editor was used?

 

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/#findComment-554431
Share on other sites

Okay I got it. 

 

<?php
$file = file_get_contents ('yourlinkhere');
$file = ereg_replace("\n", " ", $file);

$a = explode(' ',$file); // explode each space separated set into an array
// for each position in the array...
foreach ($a as $b) { 
   $c[] = explode(',',$b); // explode each comma separated set into a nested array, making a 2d array overall
}

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/#findComment-554600
Share on other sites

\n signifies an internal line break for some OS's.  You know how when you open up your editor and you write a line of something and then press enter and go to the next line? like:

 

blah blah blah blah

more blah blah blah

 

Well that stuff is stored in the file as one string of text, with line breaks like this:

 

blah blah blah blah\nmore blah blah blah

 

But we don't see it like that the computer knows you want to see your two lines of blah. The \n is invisible to us. But the thing is, different OS's use different things to specify end of line, so if you were to try to open a text file in windows that was made in a unix environment for example, you would not get your formatted lines it would be one long line and where the linebreaks are supposed to be, it would show some default character signifying it's a non-printable character, like a black square, or just a plain blank space, or whatever. 

 

Most ftp programs will automatically convert the line breaks to the target OS's line break system, transferring them in "ASCII mode."  So...my guess is that the person who made the .html page has a different OS than the server and when he uploaded it to the server, for whatever reason, it did not xfer it in ASCII mode so the line breaks did not convert properly.

 

Since the browser ignores and displays \n as a blank space, it looks okay to the eye, and you can highlight, copy/paste and the copy/pasted will indeed just be blank spaces, so it was parsing correctly from that.  But if you do a view source on it, the editor knows that \n is a line break, so it shows up formatted as such.  Also, when you do a string compare, the get_contents was coming up shorter, because all the blank spaces didn't actually exist, and therefore it wasn't parsing properly. So basically what solved it was to simply replace the \n's with real blank spaces and there you have it.

Link to comment
https://forums.phpfreaks.com/topic/108073-solved-php-string/#findComment-554841
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.