0

i searched Google but found nothing what fits for my problem, or i search with the wrong words.

In many threads i read, the smarty Template was the solution, but i dont wont use smarty because its to big for this little project.

My problem:

I got a CSV file, this file contents only HTML and PHP code, its a simple html template document the phpcode i use for generating dynamic imagelinks for example.

I want to read in this file (that works) but how can i handle the phpcode inside this file, because the phpcode shown up as they are. All variables i use in the CSV file still works and right.

Short Version

how to handle, print or echo phpcode in a CSV file.

thanks a lot,

and sorry for my Bad english

raaskler
  • 17
  • 1
  • 2
    Welcome to Stack Overflow! This question is a little short on information. Can you share what you have tried, and what problems have you run into? – Jay Blanchard Feb 24 '15 at 18:59
  • Have you tried parsing the CSV rows into strings, then replacing the with a eval($phpCode)? – Don Boots Feb 24 '15 at 18:59
  • 1
    *"because the phpcode shown up as they are."* - Is your file a `.php` extension and is PHP running on the server you're on? @JayBlanchard *Gidday Sam* – Funk Forty Niner Feb 24 '15 at 19:06
  • Hi, thanks for the fast answers. @Don Boots no i dont tried this, but it sounds good ... will try this – raaskler Feb 24 '15 at 19:24
  • 1
    You should include examples of your CSV source. – Don Boots Feb 24 '15 at 19:31
  • Did a couple quick experiments, the fact that `fgetcsv` struggles with PHP content inside a CSV is just another fine example of the paltry CSV support PHP provides. I found [other issues](http://stackoverflow.com/questions/26840256/fgetcsv-fputcsv-escape-parameter-fundamentally-broken) a couple months ago myself. #disappointed... – quickshiftin Feb 24 '15 at 19:37
  • `$userdatei = fopen("selltemplate/template.txt","r"); while(!feof($userdatei)) { $zeile = fgets($userdatei); echo $zeile; } fclose($userdatei);` so i read in the csv file and the content of csv file one line: `src="<?php echo $bild1; ?>" ></a>` – raaskler Feb 24 '15 at 20:01
  • txt or csv have forgot to rename it sry – raaskler Feb 24 '15 at 20:01
  • were you able to resolve this? – Don Boots Feb 26 '15 at 16:12

2 Answers2

0

Formatting your comment above you have the following code:

$userdatei = fopen("selltemplate/template.txt","r");

while(!feof($userdatei)) {
   $zeile = fgets($userdatei);
   echo $zeile;
}

fclose($userdatei);

// so i read in the csv file and the content of csv file one line:
// src=&quot;&lt;?php echo $bild1; ?&gt;&quot; &gt;&lt;/a&gt;

This is assuming $bild1 is defined somewhere else, but try using these functions in your while loop to parse and output your html/php:

$userdatei = fopen("selltemplate/template.txt","r");

while(!feof($userdatei)) {
   $zeile = fgets($userdatei);
   outputResults($zeile);
}

fclose($userdatei);

//-- $delims contains the delimiters for your $string. For example, you could use <?php and ?> instead of &lt;?php and ?&gt;
function parseString($string, $delims) {
    $result = array();

    //-- init delimiter vars
    if (empty($delims)) {
        $delims = array('&lt;?php', '?&gt;');
    }

    $start = $delims[0];
    $end = $delims[1];

    //-- where our delimiters start/end
    $php_start = strpos($string, $start);
    $php_end = strpos($string, $end) + strlen($end);

    //-- where our php CODE starts/ends
    $php_code_start = $php_start + strlen($start);
    $php_code_end = strpos($string, $end);

    //-- the non-php content before/after the php delimiters
    $pre = substr($string, 0, $php_start);
    $post = substr($string, $php_end);

    $code_end = $php_code_end - $php_code_start;
    $code = substr($string, $php_code_start, $code_end);

    $result['pre'] = $pre;
    $result['post'] = $post;
    $result['code'] = $code;

    return $result;
}

function outputResults($string) {
    $result = parseString($string);

    print $result['pre'];
    eval($result['code']);
    print $result['post'];
}
Don Boots
  • 2,028
  • 2
  • 18
  • 26
0

Having PHP code inside a CSV file that should be parsed and probably executed using eval sounds pretty dangerous to me.

If I get you right you just want to have dynamic parameters in your CSV file right? If thats the case and you don't want to implement an entire templating language ( like Mustache, Twig or Smarty ) into your application you could do a simple search and replace thing.

$string = "<img alt='{{myImageAlt}}' src='{{myImage}}' />";

$parameters = [
    'myImageAlt' => 'company logo',
    'myImage' => 'assets/images/logo.png'
];

foreach( $parameters as $key => $value )
{
    $string = str_replace( '{{'.$key.'}}', $value, $string );
}
Mario
  • 3,339
  • 2
  • 22
  • 41