Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Add a prefix and suffix to a text string with PHP

Posted on 2008-11-18
4
Medium Priority
?
553 Views
Last Modified: 2012-06-27
I need to generate some HTML code for use in a VBA script.

I therefore need to add a " to the beginning of each text line and a " & _ to the end..

e.g my source HTML string is

<table cellpadding=0 cellspacing=0 align=left> and should become

"<table cellpadding=0 cellspacing=0 align=left>" & _

I have to read each line of source code from a text file and output it to a new file.

I'm stuck.
0
Comment
Question by:chrisfixit
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
cr4ck3rj4ck earned 2000 total points
ID: 22984348
Hi there,

Hope this helps,
CJ
<?php
	$items = file('input.txt');
	$file = fopen('output.txt', 'w');
	foreach($items as $data)
	{	
		$str = '"'.trim($data).'" & _';
		fwrite($file, $str."\n");
	}	
	fclose($file);
?>

Open in new window

0
 
LVL 16

Expert Comment

by:alain34
ID: 22984368
Hello chrisfixit,

Sorry, I haven't tested it, but it is what I will do to resolve your request!

$inputFile = "YourFile.txt";
$outputFile = "YourOutputFile.txt";
$handle = fopen($inputFile,'r');
$handle2 = fopen($outputFile,'w');

while (!feof($handle)) {
   $buffer = fgets($handle);
   $buffer = "\"" . $buffer . "\" &_";
   fwrite($handle2,$buffer);
   }

fclose($handle);
fclose($handle2);

Regards,

alain34
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22984406
The PHP concatenation operator is the dot (period).

$a = 'A';
$b = 'B'
$ab = #a . $b; // PRODUCES 'AB'

$a .= 'BCD'; // PRODUCES 'ABCD'
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22984410
Opps, sorry for the typo.

$ab = $a . $b; // PRODUCES 'AB'
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question