here is my code. What I am trying to achieve is get text like this
Hola Hi
Pollo Chicken
Queso Cheese
and so on, and be able to make an array out of it such that
array[0][1] is Hi.
here is my code, the error is on line 13
<?php
if(isset($_POST['submit'])){
$message = $_POST['text'];
$words2 = explode("\r\n", $message);
$words = explode("\t", $words2[0]);
$numberoflines = count($words2);
echo $numberoflines;
for($i=0; $i<$numberoflines; $i++){
$words[$i] = $line;
$arrayline = explode("\t", $line);
$cow = array(
for($u=0; $u<2; $u++){
array($arrayline[$u])
}
);
}
}
?>
<html>
<form method = "POST" method ="changetext.php">
<textarea name="text">
</textarea>
<input type="submit" value = "Flip!" name="submit">
</form>
</html>
Maybe thats what you wanted to achieve ?!?
for($i=0; $i<$numberoflines; $i++){
$arraycols= explode("\t", $words[$i]);
foreach($arraycols as $col){
$list[$i][] = $col;
}
}
so Array $list is $list[row][col]
if i got right what is inside the $words Array. Your code is a little messi ;)
Try something like this:
$words = array();
if(isset($_POST['submit'])){
// Break down the text as lines:
$lines = explode("\r\n", $_POST['text']);
// For every line...
foreach($lines as $line){
// Seperate the 2 words (seperated by a tab)
$words[] = explode("\t", $line);
}
// Print the result:
var_dump($words);
}
Related
I have a constantly updated URL .txt file and I would like to grab the first and third words from every line where the tenth word is "B738". The words are spearated with ':'
Example from the file, here is 1 line: (yes, it is 1 line)
RYR98G:1374442:First Last:PILOT::36.50552:-13.87929:31183:451:B738:438:LPFR:31000:LPMA:UK1:100:1:7010:::1:I:0:0:1:41:3:30:GCXO:+VFPS+/V/PBN/A1B1C1D1S1S2 DOF/190521 REG/EIEFF OPR/RYR PER/C RMK/TCAS:N0438F310 IXOL9V IXOLI DCT LATRU DCT VERAM Q205 PECKY UQ146 EPAKA EPAK1X:0:0:0:0:::20190521084125:215:30.121:1020:
I would like to grab the "RYR98G" and the "First Last" because the tenth word is B738
<?php
$text = file_get_contents('http://data.vattastic.com/vatsim-data.txt');
$lines = explode("\n", $text);
$skipped_content = implode("\n", array_slice($lines, 62));
echo nl2br($skipped_content);
?>
Something like this should do what you want:
foreach ($lines as $line) {
$words = explode(':', $line);
if (isset($words[9]) && $words[9] == 'B738') {
echo "First: $words[0]\nThird: $words[2]\n";
}
}
Output (for your sample line):
First: RYR98G
Third: First Last
Demo on 3v4l.org
<?php
$text = file_get_contents('http://data.vattastic.com/vatsim-data.txt');
$lines = explode(":", $text);
if( $lines[9] == "B738" ){
echo $lines[0]; //RYR98G
echo $lines[2]; //First Last
}
?>
Here the stript, you need to load the content of your txt, in a variable.
<?php
$input_lines = "RYR98G:1374442:First Last:PILOT::36.50552:-13.87929:31183:451:B738:438:LPFR:31000:LPMA:UK1:100:1:7010:::1:I:0:0:1:41:3:30:GCXO:+VFPS+/V/PBN/A1B1C1D1S1S2 DOF/190521 REG/EIEFF OPR/RYR PER/C RMK/TCAS:N0438F310 IXOL9V IXOLI DCT LATRU DCT VERAM Q205 PECKY UQ146 EPAKA EPAK1X:0:0:0:0:::20190521084125:215:30.121:1020:"; // example of line
$matches = array(); //Array
$search = preg_match_all('/(.*?):(.*?)/im', $input_lines, $matches); //regex matching
if($matches[1][9] == "B738") //target :)
{
print_r('B738 Found! <br />');
print_r('Item 1!'.$matches[1][0].'<br />');
print_r('Item 2!'.$matches[1][3].'<br />');
}
// Debug of your array. As you can see all items matched.
foreach ($matches[1] as $match) {
echo " Item: ".$i."<br />";
echo $match;
$i++;
}
I've got a "data.txt" file with the following content:
One1:One2:One3:One4:One5:One6
Two1:Two2:Two3:Two4:Two5:Two6
Three1:Three2:Three3:Three4:Three5:Three6
Now I want to be able to take each data and put on a specific location of a htlm code. Each line for its own. So for the first line, it should look something like this:
<html>
<head>
<title></title>
<head>
<body>
<h1>One2</h1>
<h2>One4 some other Text One5</h2>
<img src="One6.jpg">
</body>
</html>
Unforturtunately I don't have a clue how to do that with explode(). Can anybody help me out or does someone know a good and easy tutorial?
Thanks a lot
Not completely sure if I got your question, but I think you want to first parse the data.txt line by line, THEN by the : delimiter.
$lines = file("data.txt", FILE_IGNORE_NEW_LINES);
foreach($lines as $line)
{
$split = explode(":", $line);
$s1 = $split[0];
$s2 = $split[1];
$s3 = $split[2];
echo '<h1>'.$s1.'</h1>';
echo '<h2>'.$s2.' lorem ipsum</h2>';
echo '<img src="'.$s3.'">';
}
Or would you like to output different content based on what line its currently reading from? Then something like:
$lines = file("data.txt", FILE_IGNORE_NEW_LINES);
$n = count($lines);
for($i=0; $i<$n; $i++)
{
$split = explode(":", $lines[$i]);
$s1 = $split[0];
$s2 = $split[1];
$s3 = $split[2];
if($i === 0)
{
echo 'First line in data.txt<br>';
}
elseif($i === 1)
{
echo 'Second line in data.txt<br>';
}
elseif($i === 2)
{
echo 'Third line in data.txt<br>';
}
else
{
echo 'All the other lines (which are currently not existing)<br>';
}
}
Just kind of guessing here, could need some more information to clarify the question :)
I am trying to export all domains located in domains.txt file that match exactly with the links from the all-urls.txt file. This is my script:
<?php
if (isset($_POST['submit'])) {
$badwords = file('domains.txt', FILE_SKIP_EMPTY_LINES);
$domains = file('all-urls.txt', FILE_SKIP_EMPTY_LINES);
$newarr = array();
echo '<table>';
foreach($badwords as $k=>$v) {
foreach($domains as $k1=>$v1) {
if(strpos($v1,$v)!==false) {
array_push($newarr,$v1);
$links = array_shift($newarr);
echo '<tr><td>';
echo $links;
echo '</td></tr>';
}
}
}
echo '</table>';
}
?>
<body>
<form action="" name="submit">
<p><label>Ready to submit</label></p>
<p><input name="submit" type="submit" value ="Go"></p>
</form>
</body>
Instead of $badwords = file('domains.txt', FILE_SKIP_EMPTY_LINES);
I had
$badwords = array('domain1.com', 'domain2.com');
and instead of
$domains = file('all-urls.txt', FILE_SKIP_EMPTY_LINES);
I had
$domains = array('domain1.com/url/subfoler.html', 'domain1.com/url/subfoler.html','domain2.com/sublink/otherthings.php');
And right now i am trying to replace the array with file because it's way easier for me to load them like this because i have a large number of domains and url.
The problem is that the script doesn't do anything in this form. Where am i mistaking
How do you know that you actually have read the file?
$badwords = file('domains.txt', FILE_SKIP_EMPTY_LINES);
if ( $badwords === false )
die ("error on file " . 'domains.txt');
$domains = file('all-urls.txt', FILE_SKIP_EMPTY_LINES);
if ( $domains === false )
die ("error on file " . 'all-urls.txt');
...
additionally, the expressions
array_push($newarr,$v1);
$links = array_shift($newarr);
are quite the same as
$newarr = array();
$links = $v1;
and use some cpu cycles with no effect at all ...
The default method to post a form, is GET, so if you don't specify a method, GET will be used and you will never enter the section where you compare the files as:
isset($_POST['submit'])
is false
You can add a method to the form to solve that:
<form action="" name="submit" method="post">
Found my answer:
<?php
$domains = file("domains.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$urls = file("all-urls.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$newarr = array();
$f = fopen("result.txt", w);
echo "<table>";
foreach($domains as $k=>$v) {
foreach($urls as $k1=>$v1) {
if(strpos($v1,$v)!==false) {
array_push($newarr,$v1);
$links = array_shift($newarr);
echo "<tr><td>";
echo $links;
echo "</td></tr>";
fwrite($f, "$links\r\n");
}
}
}
echo "</table>";
?>
I needed to add the FILE_IGNORE_NEW_LINES, because the urls are placed in new lines.
<?php
$userName = array();
$tutorial = array();
$myFile = "students.txt";
$fh = fopen($myFile,'r');
while( !feof($myFile) ){
$userName[] = array(fgets($fh));//Save first line content
$tutorial[] = array(fgets($fh));//Save second line content
}
fclose($myFile);
echo "$userName";
echo "$tutorial";
?>
and my students.txt content
dasdsa
A
asdasd
D
How to read that and store into different array and print them out
your code should work as expected. I assume you're bit confused with echo "$userName"; output as it displays Array word. try var_dump($userName) instead
Do exactly as you've done, but change
$userName[] = array(fgets($fh));//Save first line content
$tutorial[] = array(fgets($fh));//Save second line content
to
$userName[] = fgets($fh);//Save first line content
$tutorial[] = fgets($fh);//Save second line content
(no need to keep the subitems in their own seperate array)
and print them out by either using print_r, or iterate through them:
for ($i = 0; $i < count($userName); $i++) {
echo $userName[$i] . " - " . $tutorial[$i];
}
$text = file_get_contents('students.txt');
$text = explode("\n",$text);
$output = array();
foreach($text as $line)
{
$output[] = $line;
}
Use function file() in PHP
file — Reads entire file into an array
$array_lines = file('students.txt');
$count = count($array_lines);
$first_arr = array();
$sec_arr = array();
foreach ($array_lines as $i => $line){
if($i%2) $first_arr[] = $line;
else $sec_arr[] = $line;
}
print_r($first_arr);
print_r($sec_arr);
With file() function every line is read as element in array. You can check it with:
print_r($first_arr);
I have following example PHP code and its working great! but i just want one more addition so it print line number too.
<?php
$path = shell_exec('cat data.txt');
$path = chop($path,"\n");
$lines = explode("\n",$path);
echo "<h2>List of Studies</h2>";
foreach($lines as $line) {
echo "<h3><p>$line</p></h3>";
}
?>
Output:
ABC
XYZ
123
I want following addition and add counter in it.
1. ABC
2. XYZ
3. 123
You can assign the index to variable too, not just the value:
foreach($lines as $index => $line) {
printf('<h3><p>%d. %s</p></h3>', $index + 1, $line);
}
If you don't want to use the index php, you can use the tag ol
Reff: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_lists
$i=1;
foreach($lines as $line) {
echo "<h3><p>$i. $line</p></h3>";
$i++;
}
<?php
$path = shell_exec('cat data.txt');
$path = chop($path,"\n");
$lines = explode("\n",$path);
echo "<h2>List of Studies</h2>";
$c = 0;
foreach($lines as $line) {
$c++
echo "<h3><p>".$c.". ".$line."</p></h3>";
}
?>
http://www.php.net/manual/en/control-structures.foreach.php
foreach($lines as $i=>$line) {
echo "<h3>$i. <p>$line</p></h3>";
}