• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

add characters on each line end

Hi experts,

I need a php script that adds specific characters on every line end.

Thanks.
0
gloriaewold41
Asked:
gloriaewold41
  • 7
  • 6
  • 4
  • +1
1 Solution
 
PsyberionCommented:
Do you have any more details? Is this to add to an existing script or you want a complete script that just does this? What lines do you want to add the characters to? Is it lines in a specified file? Does it just need to display the lines with the added text, output it to another file? update the same file? display it on a web page? Etc.
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Where does this line is ?
Are you reading from an external file line by line ?

(PS:Please ask more specified questions we cannot guess your code :(  )
0
 
Ray PaseurCommented:
... on every line end.
In PHP, "line" is not a term of art so it is hard to discern what might be going on here.  But we can probably help.  Please post a small sample of the code that demonstrates the error condition, and please post the test data that you have used with the sample code.  Armed with that information we can almost certainly show you a "best practices" solution.  

The PHP end-of-line character is a context-sensitive constant named PHP_EOL.  You can use str_replace() with this constant to add characters to each end of line.
http://php.net/manual/en/function.str-replace.php
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
gloriaewold41Author Commented:
I want a complete script that adds characters to every line end (no space between line and add) from a specified file and output to another file. Hope this is complete.

unedited line:
name, address, zip, phone

and i need to add website name at the end of every line like this:
name, address, zip, phone|website.com
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Please read our posts
0
 
gloriaewold41Author Commented:
What do you mean erdincgc?
0
 
Ray PaseurCommented:
See http://www.laprbass.com/RAY_temp_gloriaewald41.php

<?php // RAY_temp_gloriaewald41.php
error_reporting(E_ALL);

// FROM THE POST AT EE
$original = 'name, address, zip, phone';

// SET THE DELIMITER PER THE POST AT EE
$delim = '|';

// SET THE APPENDED STRING PER THE POST AT EE
$append = 'website.com';

// USE PHP CONCATENATION OPERATOR
$new = $original . $delim . $append;

// SHOW THE WORK PRODUCT
echo "FROM $original WE CREATED $new";

Open in new window

HTH, ~Ray
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
I'd asked some questions to learn more about your requirement. Also Ray requested a sample for the same reasons. But you just tell us a little bit more about questions.

What will php produce ? a txt file , a xhtml or just dump to screen ?
What will be before the  characters to be added on each lines end ?

We are not Nostradamus and cannot see what's on your mind. So please tell us the full story and if you can supply some workaround code.

We can't help you without these informations.
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
You are great RAY ...
It seems Nostradamus visited you :)
0
 
gloriaewold41Author Commented:
Ray, i don't understand it. I need it to edit every line in a file.
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
ok thats it :)

try this ;

<?php
    $file_path_and_name="path/file_name";// SET THESE AS YOU WISH
	$append_txt="ADDED_TXT";
	$read_type=1;
    if ( file_exists("$file_path_and_name")){
         if (is_file ("$file_path_and_name")){

            if($read_type==1){
                //read file line by line
                while ( !feof ($file_path_and_name ) )
                {
                $the_line = fgets ( $file_path_and_name,1024 );
                $output.=$the_line.$append_txt;
                }
            }
		 }
	}
	echo $output;
?>

Open in new window

0
 
gloriaewold41Author Commented:
it's a .txt file with

name, address, zip, phone
name, address, zip, phone
name, address, zip, phone
name, address, zip, phone
etc

and need to add "|website.name" at end of each line and look like this

name, address, zip, phone|website.name
name, address, zip, phone|website.name
name, address, zip, phone|website.name
name, address, zip, phone|website.name

and output to text file or like in ray's script
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Thanks for details. if you wish to overwrite file after appending use this. (file must be writable)

<?php
    $file_path_and_name="path/file_name";// SET THESE AS YOU WISH
    $append_txt="ADDED_TXT";
    $read_type=1;
    $write_type="overwrite";

    if ( file_exists("$file_path_and_name")){
         if (is_file ("$file_path_and_name")){

            if($read_type==1){
                //read file line by line
                while ( !feof ($file_path_and_name ) ) {
                     $the_line = fgets ( $file_path_and_name,1024 );
                     $output.=$the_line.$append_txt;
                    }
                }

               if($write_type=="overwrite"){
                  $file_work = fopen("$file_path_and_name",'w') or die ("fail to write") ;
                  fwrite($file_work,  $output);
                  fclose($file_work);
                  return TRUE;
                }
            }
       }
echo $output;
?>

Open in new window

0
 
gloriaewold41Author Commented:
It doesn't work erdincgc, tried in browser and linux command line.
0
 
Ray PaseurCommented:
To read every line in a file, you might want to use file().
http://us.php.net/manual/en/function.file.php

To access every line in the array, you might want to use foreach().
http://us.php.net/manual/en/control-structures.foreach.php

To create an output string from the array you might want to use implode(PHP_EOL).
http://us.php.net/manual/en/function.implode.php

To write a file back to the file system you might want to use file_put_contents().
http://us.php.net/manual/en/function.file-put-contents.php
0
 
Ray PaseurCommented:
And if you're still having trouble after you read the descriptions and examples for those functions, please post the code you have written and tried.  We can usually answer questions and sometimes help debug, but we are unpaid volunteers, and we cannot write your code for you.  For something like that, ELance.com or a similar site would be a good place to hire a professional programmer.

Best regards, ~Ray
0
 
PsyberionCommented:
Is the 'website.name' the same for every 'name,address,zip,phone'? Also, why write a PHP script to do this? If your details are already in a comma separated value file (CSV), simply load it into excel, add the website name to the next blank column (if it is the same for every line then you could type it once and drag it down for all rows, but I suspect it isn't) and then simply save it as a CSV file again. No script writing needed...
0
 
gloriaewold41Author Commented:
I got it. Thanks

<?php
error_reporting(E_ALL);
echo "<pre>";

$file = <<<ENDFILE
texthere
ENDFILE;

$add = 'add';

echo PHP_EOL;
var_dump($file);
echo PHP_EOL;

$arr = explode(PHP_EOL, $file);
foreach ($arr as $key => $str)
{
    $str = trim($str);
    $arr[$key] = $str . $add;
}
$new = implode(PHP_EOL, $arr);

echo PHP_EOL . "NEW FILE:" . PHP_EOL;
var_dump($new);
?>
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Here is the tested solution with file update

<?php
	$output_to_file="";	$output_to_screen="";
	
    $file_path_and_name="myfile.txt";// SET AS YOU WISH
    $append_txt=" -> THIS IS ADDED_TXT";

	
    if ( file_exists("$file_path_and_name")){
         if (is_file ("$file_path_and_name")){

				$lines = file("$file_path_and_name");
				foreach($lines as $the_line){
					
					if(strpos($the_line,PHP_EOL)) $output_to_file.=str_replace(PHP_EOL,$append_txt,$the_line).PHP_EOL;
					else $output_to_file.=$the_line.$append_txt;
					//delete these if you wont return screen output
					if(strpos($the_line,PHP_EOL)) $output_to_screen.=str_replace(PHP_EOL,$append_txt,$the_line)."<br>"; 
					else $output_to_screen.=$the_line.$append_txt;
				}
			  //update file content	
			  $file_work = fopen("$file_path_and_name",'w') or die ("failed to write") ;
			  fwrite($file_work,  $output_to_file);
			  fclose($file_work);
			  return TRUE;
        }
    }
echo $output_to_screen;
?>

Open in new window

test-file-write.php
myfile.txt
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 7
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now