Add a prefix and suffix to a text string with PHP

Posted on 2008-11-18
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.
Question by:chrisfixit
    LVL 6

    Accepted Solution

    Hi there,

    Hope this helps,
    	$items = file('input.txt');
    	$file = fopen('output.txt', 'w');
    	foreach($items as $data)
    		$str = '"'.trim($data).'" & _';
    		fwrite($file, $str."\n");

    Open in new window

    LVL 16

    Expert Comment

    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 . "\" &_";



    LVL 107

    Expert Comment

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

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

    $a .= 'BCD'; // PRODUCES 'ABCD'
    LVL 107

    Expert Comment

    by:Ray Paseur
    Opps, sorry for the typo.

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

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    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…
    The viewer will learn how to dynamically set the form action using jQuery.

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now