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>";
}
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++;
}
My code is receiving a string which I have no control of, which I'll call $my_string. The string is the contents of a transcript. If I echo the string, like so:
echo $my_string;
I can see the contents, which look something like this.
1
00:00:00.000 --> 00:00:04.980
[MUSIC]
2
00:00:04.980 --> 00:00:08.120
Hi, my name is holl and I am here
to write some PHP.
3
00:00:08.120 --> 00:00:10.277
You can see my screen, here.
What I'd like to do is run this through a function so it's just the actual words spoken, removing all the lines that signify time, or the order.
[MUSIC]
Hi, my name is holl and I am here
to write some php.
You can see my screen, here.
My idea is to explode the whole string by the break, and try to detect which lines which are either empty or start with a number, like so...
$lines = explode("\n", $my_string);
foreach ($lines as $line) {
if (is_numeric(line[0]) || empty(line[0]) ) {
continue;
}
$exclude[] = $line;
}
$transcript = implode("\n", $exclude);
But the result of this action is exactly the same- the output has numbers and blank lines. I clearly misunderstand something- but what is it? And is there a better way of trying to achieve my goal?
Thanks!
EDIT: Removed an echo where I wasn't actually using one in my code.
The problem is that you use indexing on $line:
$lines = explode("\n", $my_string);
foreach ($lines as $line) {
if (is_numeric(line[0]) || empty(line[0]) ) {//index usage?
continue;
}
$exclude[] = $line;
}
$transcript = echo implode("\n", $exclude); //remove echo
replace by:
$lines = explode("\n", $my_string);
foreach ($lines as $line) {
if (is_numeric($line) || empty($line) ) {//here
continue;
}
$exclude[] = $line;
}
$transcript = implode("\n", $exclude);
You also need regex matching to remove the 00:00:00.000 --> 00:00:04.980 fragments.
You can combine them by:
if(preg_match('/^(|\d+|\d+:\d+:\d+\.\d+\s+-->\s+\d+:\d+:\d+\.\d+)$/',$line)) { //regex
takes all possibilities into account:
$lines = explode("\n", $my_string);
foreach ($lines as $line) {
if(preg_match('/^(|\d+|\d+:\d+:\d+\.\d+\s+-->\s+\d+:\d+:\d+\.\d+)$/',$line)) {
continue;
}
$exclude[] = $line;
}
$transcript = implode("\n", $exclude);
echo $transcript;
Example (with php -a):
$ php -a
php > $my_string='1
php ' 00:00:00.000 --> 00:00:04.980
php ' [MUSIC]
php '
php ' 2
php ' 00:00:04.980 --> 00:00:08.120
php ' Hi, my name is holl and I am here
php ' to write some PHP.
php '
php ' 3
php ' 00:00:08.120 --> 00:00:10.277
php ' You can see my screen, here.';
php > $lines = explode("\n", $my_string);
php > foreach ($lines as $line) {
php { if(preg_match('/^(|\d+|\d+:\d+:\d+\.\d+\s+-->\s+\d+:\d+:\d+\.\d+)$/',$line)) {
php { continue;
php { }
php { $exclude[] = $line;
php { }
php > $transcript = implode("\n", $exclude);
php > echo $transcript;
[MUSIC]
Hi, my name is holl and I am here
to write some PHP.
You can see my screen, here.
Your code works almost. Just forgot $ in line[0] and " " is not empty().
$my_string = <<< EOF
1
00:00:00.000 --> 00:00:04.980
[MUSIC]
2
00:00:04.980 --> 00:00:08.120
Hi, my name is holl and I am here
to write some PHP.
3
00:00:08.120 --> 00:00:10.277
You can see my screen, here.
EOF;
$lines = explode("\n", $my_string);
foreach ($lines as $line) {
$temp = trim($line[0]);
if (is_numeric($temp) || empty($temp) ) {
continue;
}
$exclude[] = $line;
}
$transcript = implode("\n", $exclude);
echo $transcript;
Result:
[MUSIC]
Hi, my name is holl and I am here
to write some PHP.
You can see my screen, here.
It looks like it's a pattern. That is every first and second line contain meta data, the third is text, and the fourth is empty. If that is indeed the case, it should be trivial. You don't have to check the content at all and just grab the third line of every quartet:
$lines = explode("\n", $my_string);
$texts = array();
for ($i = 0; $i < count($lines); $i++) {
if ($i % 4 == 2) { // Index of third line is 2, of course.
$texts[] = $lines[i];
}
}
$transcript = implode($texts, "\n");
With alternative logic, because as you rightfully mentioned there can be more than one line of text, you could say that blocks/entries whatever you call them, are separated by an empty line. Each block starts with two lines of meta data, followed by one (or maybe zero) or more lines of text. With that logic you could parse it like this:
$lines = explode("\n", $my_string);
$texts = array();
$linenr = 0;
foreach ($lines as $line) {
// Keep track of the how manieth non-empty line it is.
if ($line === '')
$linenr = 0;
else
$linenr++;
// Skip the first two lines of a block.
if ($linenr > 2)
$texts[] = $line;
}
$transcript = implode($texts, "\n");
I don't know this particular format, but if I wanted to do this, I would be eager to find a pattern like this rather than parse the lines themselves. It looks like a script or subtitle file, and if you want to turn it into a transcript, it would be a shame if somebody shouted '300' and it would not be transcripted.
to remove theses lines try to use : preg_replace + regex
php man [1]: http://php.net/manual/en/function.preg-replace.php
$file=fopen("question.txt","r");
while(!feof($file))
{
echo "<h3>". fgets($file)."</h3>"."<br />";
for($i=0;$i<=3;$i++)
{
echo fgets($file)."<br />";
}
}
$lines =file("filename.txt");
and then $lines[4] will return you fifth line.
Find and replace the line. Look at str_replace function at http://php.net/manual/en/function.str-replace.php :)
Maybe a better option in this case... Use the file($path) function to get the lines into an array, then loop through it.
$lines = file($path, FILE_IGNORE_NEW_LINES);
$remove = "balblalbllablab";
foreach($lines as $key => $line)
if(stristr($line, $remove)) unset($lines[$key]);
$data = implode('\n', array_values($lines));
$file = fopen($path);
fwrite($file, $data);
fclose($file);
<?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 a problem where i have a script that supposed to search an XML file for matching strings.
Here's my script:
<?php
$file = "klein.xml";
$lines = file($file);
foreach ($lines as $line_num => $line)
{
echo htmlentities($line);
echo "<br>";
}
if (preg_match("/message/", htmlentities($line), $found))
{
echo "Found!";
echo $found[0];
echo "test";
}
else
{
echo "Not Found!";
}
?>
and this is the xml file im working with:
<?xml version="1.0" encoding="ISO-8859-1"?>
<product-data>
<message-header>
<message-id>OR-1361163557-gr</message-id>
<message-timestamp>1361163557</message-timestamp>
<export-version current_version="1">OGPDX-2.01.01</export-version>
</message-header>
</product-data>
The problem is when i preg match 'product' it works correctly but when i try to preg match another string i.e 'message' it doesnt work?
Im curious for the solution, thanks in advance!
Can you check this code, I noticed that you put the if loop outside foreach; hence only the last line is executed.
<?php
$file = "klein.xml";
$lines = file($file);
foreach ($lines as $line_num => $line)
{
if (preg_match("/message/", htmlentities($line), $found))
{
echo "Found ".$found[0]."<br />";
}
}
?>
the problem is with your logic: you are only checking the last value of $line which is contain </product-data>
so what are you searching in? a line of your xml ...
when you are searching "product" (in last line) $line is in last line .
foreach ($lines as $line_num => $line)
{
if (preg_match("/message/", $line , $found))
{
echo "Line: $line_num - ".$found[0]." <br>";
}
}