i have the following code
$contents = file_get_contents('folder/itemtitle.txt');
$fnamedata = file_get_contents('folder/fname.txt');
$fnamearray = explode("\n", $fnamedata);
$contents = explode("\n", $contents);
foreach ($contents as $key => $itemline)
{
}
foreach ($fnamearray as $key2 => $fname)
{
echo ($fname);
echo ($itemline);
}
what i want to do is to have the first line of each file echo so the output looks like
fname[0},itemline[0],fname[1],itemline[1]
what i am getting with the following is just this
fname[0],fname[1],fname[2].... ect
h
Assuming the indexes will always match:
$contents = file_get_contents('folder/itemtitle.txt');
$fnamedata = file_get_contents('/home/b1396hos/public_html/ofwgkta.co.uk/dd_folder/fname.txt');
$fnamearray = explode("\n", $fnamedata);
$contents = explode("\n", $contents);
for($i = 0; $i < count($contents); $i++)
{
echo $fnamearray[$i];
echo $contents[$i];
}
Since both arrays are simple, consecutive numeric indexed arrays, you can just use a for loop:
$l = max(count($fnamedata),count($contents));
for($i=0; $i<$l; $i++) {
$itemline = $contents[$i];
$fname = $fnamearray[$i];
// do stuff
}
Related
I have a string like
$totalValues ="4565:4,4566:5,4567:6";
Now i want only values after ':' with coma separated ,i.e i want string like $SubValues = "4,5,6"
Thanks
using array_map, implode and explode
$totalValues ="4565:4,4566:5,4567:6";
echo implode(',',array_map(function($val){
$val = explode(':',$val);
return $val[1];
},explode(',',$totalValues)));
try this code it is working
<?php
$str="4565:4,4566:5,4567:6";;
$data =explode(",", $str);
echo '<pre>';
print_r($data);
$newstring="";
foreach ($data as $key => $value) {
preg_match('/:(.*)/',$value,$matches);
$newstring.=$matches[1].",";
}
echo rtrim($newstring,",");
Output
i have updated my code please check it
<?php
$str="4565:4,4566:5,4567:6";
$data1=explode(",", $str);
$newstring1="";
foreach ($data1 as $key1 => $value) {
preg_match('/(.*?):/',$value,$matches);
$newstring1.=$matches[1].",";
}
echo rtrim($newstring1,",");
you might want it this way:
$totalValues ="4565:4,4566:5,4567:6";
$temp = explode(':', $totalValues);
$subValues = $temp[1][0];
$x = count($temp);
for ($i = 2; $i < $x; $i++) {
$subValues .= ',' . $temp[$i][0];
}
echo $subValues;
the code below works for the first word on each line, but how do I get the first number on each line? please keep in mind that the number can be 1 digit or it can be 20 digits long
<?php
$file = new SplFileObject("/tmp/test.txt", "r");
$data = array();
while(! $file->eof()) {
$data[] = array_shift(($file->fgetcsv("|")));
}
echo implode(",", $data);
?>
This gets the first number in a string:
function getFirstNumber($str){
$strlen = strlen( $str );
$num = "";
for( $i = 0; $i <= $strlen; $i++ ) {
$char = substr($str, $i, 1);
if(!is_numeric( $char)){
if(!empty($num))
return intval($num);
}else $num .= $char;
}
return intval($num);
}
$str = "asdfas1234241234lkj 1l2k3j4 1k2j3412341234";
echo getFirstNumber($str);
Here's a fiddle.
The code you presented, gets the first part, not the first word.
You have to walk through the array using foreach, array_walk, ...
For example:
<?php
$file = new SplFileObject("/tmp/test.txt", "r");
$data = array();
while(! $file->eof()) {
foreach($file->fgetcsv("|") as $part) {
if (is_numeric($part)) { //or any other numeric check you like
$data[] = $part;
break;
}
}
}
echo implode(",", $data);
?>
Got this code php
$data ='
one;uno
two;dos
three;tres
four;cuatro
'
I want to print the first column in a row, separating elements with comma and aspace, to obtain this result:
one, two, three, four
Any help please? I'm doing this but I can`t:
<?php
$data ='
one;uno
two;dos
three;tres
four;cuatro
';
$line = explode("\n", $data);
for($i = 0; $i<count($line); $i++) {
$item = explode(";", $line[$i]);
$coma = implode(', ', $item[0{);
echo $coma;
}
?>
try this modified version of your code
$data ='one;uno
two;dos
three;tres
four;cuatro';
$coma=array();
$line = explode("\n", $data);
for($i = 0; $i<count($line); $i++) {
$item = explode(";", $line[$i]);
$coma[]= $item[0];
}
echo implode(',',$coma);
I am getting tr of tables and then in loop i want get text of all td fields, look here:
<?
$lines = $xpath->query("//table[#id='cab_table'] //tr");
var_dump($lines);// Give me object(DOMNodeList)#11 (1) { ["length"]=> int(6) }
for( $i = 0; $i < count($lines); $i++) {
if($i != 0){
$tds = $xpath->query('//td', $lines[$i]);
$result[$i - 1]['number'] = trim($tds->item(0)->nodeValue);
$result[$i - 1]['volume'] = trim($tds->item(1)->nodeValue);
$result[$i - 1]['sum'] = trim($tds->item(2)->nodeValue);
}
}
var_dump($result); //Give me NULL
die();
?>
Why i get NULL?
Now i have:
$lines = $xpath->query("//table[#id='cab_table'] //tr");
foreach($lines as $line) {
$tds = $xpath->query('//td', $line);
$count = $tds->length;
for($i=0; $i<$count; $i++){
echo $tds->item($i)->nodeValue.'<br>';
//echo $i.'<br>';
}
}
But i want make the next for each tr at loop $result[0] = td[0]; $result[1] = td[1]; $result[2] = td[2]; Can you advise me?
->query() returns a DOMNodeList object. It can be count()ed and foreach()ed, but you can't USE it as an array as you are.
$tds = $xpath->query('//td', $lines[$i]);
^^^^^^^^^^---incorrect
Try
$lines = $xpath->query("//table[#id='cab_table'] //tr");
foreach($lines as $line) {
$tds = $xpath->query('//td', $line);
...
}
instead.
foreach($lines as $line) {
for($j=0; $j<=3; $j++) {
$tds_{$j} = $xpath->query('//td['.$j.']', $line);
$tds_{$j} = $xpath->query('//td['.$j.']', $line);
$tds_{$j} = $xpath->query('//td['.$j.']', $line);
$count = $tds_{$j}->length;
for($i=0; $i<$count; $i++){
$this->result['number'][] = $tds_{$j}->item($i)->nodeValue;
$this->result['volume'][] = $tds_{$j}->item($i)->nodeValue;
$this->result['code'][] = $tds_{$j}->item($i)->nodeValue;
$this->result['start_date'][] = $startDate;
$this->result['end_date'][] = $endDate;
}
}
}
I have a .dat file that is essentially ; delimited file and I'm trying to convert it to a tab delimited .txt. The problem that I am not sure about is that each row of the new file will be a combination of 3 of the original file's rows, each original row has a different quantity of data. The first column just identifies each row in a grouping. What would be the best way to do this?
Sample original data:
01;zxc;asd;qwe;uio;jkl;asd;123;456
02;lkj;oiu;oji
03;fjifao;vbofekjf;fjieofk;aoijf;voien3984
01;lkj;oiu;fji;eoj;vnk;fji;098;321
02;fji;oje;jvi
03;jie;voi;djv;eojf;38723
End output:
zxc asd qwe uio jkl asd 123 456 lkj oiu oji fjifao vbofekjf fjieofk aoijf voien3984
lkj oiu fji eoj vnk fji 098 321 fji oje jvi jie voi djv eojf 38723
Any ideas?
Here's how I'd do it:
$lines = file($data);
$rows = array();
$row_pivot = -1;
foreach ($lines as $line) {
// Split line by ;
$data = explode(';', trim($line));
// Get the first element
$r_id = array_shift($data);
if ($r_id == '01') {
// When 01 is the first element, start a new row
// You can dump the previous row here as well if you aim for speed
$row_pivot++;
$rows[$row_pivot] = array();
}
// Add data to row
$rows[$row_pivot] = array_merge($rows[$row_pivot], $data);
}
// Print rows
foreach ($rows as $r) {
echo implode("\t", $r)."\n";
}
I would personally explode the data then foreach row in the resulting array, then again explode each line at your delimiter ';' and then format an output that is tab delimited.
<?php
$data = 'LOADED FILE DATA';
$lines = preg_split( '/\r\n|\r|\n/', $data);
$out = '';
foreach($lines as $line){
$parts = explode(';',$line);
foreach($parts as $part){
$out .= $part.'\t';
}
$out .= '\n';
}
echo $out;
?>
code untested.
Should be something like this
$lines = file($filename);
$lineCount = count($lines);
$output = '';
for ($i = 0; $i < $lineCount - 2; $i += 3) {
$newLines = array();
for ($j = $i; $j < $i + 3; $j++) {
list($_, $rest) = explode(';', isset($lines[$j]) ? $lines[$j] : '');
$newLines = array_merge($newLines, $rest);
}
$output .= implode("\t", $newLines) . "\n";
}