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);
Related
I have string and I want to splitt it to array and remove a specific part and then convert it back to a string .
$string = "width,100;height,8600;block,700;narrow,1;"
I want to search block in this string and remove it with its value "block,700;" and get the string as "width,100;height,8600;narrow,1;"
below is my code php which i tried.Please advice me
$outerARR = explode(";", $string);
$arr = array();
foreach ($outerARR as $arrvalue) {
$innerarr = explode(",", $arrvalue);
$arr[] = $innerarr;
}
for ($i = 0; $i < count($arr); $i++) {
if (in_array($arr, 'block')) {
unset($arr[$i]);
}
}
Please note that "block" in aboive string will not always contain and the value may differ. So I can't use string replace . please advice
You essentially want to replace part of your string:
$string = "width,100;height,8600;block,700;narrow,1;";
$regex = '#(block,(.*?);)#';
$result = preg_replace($regex, '', $string);
echo $result;
Try this:
$string = "width,100;height,8600;block,700;narrow,1;"
$outerARR = explode(";", $string);
$arr = array();
foreach ($outerARR as $arrvalue) {
$innerarr = explode(",", $arrvalue);
$arr[$innerarr[0]] = $innerarr[1];
}
if (array_key_exists('block', $arr)) {
unset($arr['block']);
}
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);
?>
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 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
}
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";
}