0

I using PHP 5.5.9-1ubuntu4.22. I am having the following csv file that I read in:

Product;Category;Manufacturer;Price;hasWlan
Product 8;Category 1;Manifacturer - 9;3;f
Product 7;Category 8;Manifacturer - 1;5;t
Product 9;Category 8;Manifacturer - 1;10;t
Product 9;Category 2;Manifacturer - 7;7;f
Product 5;Category 9;Manifacturer - 2;7;t
Product 5;Category 3;Manifacturer - 7;10;t
Product 6;Category 10;Manifacturer - 4;1;t
Product 5;Category 7;Manifacturer - 2;1;t
Product 5;Category 1;Manifacturer - 6;6;t

I would like to replace the following template by the csv values:

Template:

@attr(Product) has the @attr(Category) from the @attr(Manufacturer).

Wanted Result:

Product 8 has the Category 1 from the Manufacturer - 9.
Product 7 has the Category 8 from the Manufacturer - 1.

etc.

I tried the following php script:

<?php
class StringTemplate
{
    public function process($text, $singleVariable, $data) {

        // 1. Replace Variables
        $pattern = '@attr(' . $singleVariable . ')'; 
        $text = str_replace($pattern, $data, $text);

        // return final result
        return $text;
    }

}

/*******************/
/***EXAMPLE USAGE***/
/*******************/

// 1. Read in csv
$csvFile = '/home/ubuntu/workspace/src/SampleText.csv';

echo "\n";
$StringTemplate = new StringTemplate();
$template = "@attr(Product) has the @attr(Category) from the @attr(Manufacturer). ";

$row = 0;
$vars = array();
if (($handle = fopen($csvFile, "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
    $num = count($data);
    for ($c=0; $c < $num; $c++) {
        echo $StringTemplate->process($template, $vars, $data[$c]);
    }
    if($row == 0) {
        $vars = $data;
        echo print_r($vars) . "\n";
    }
    echo "####################### \n";
    $row++;
  }
  fclose($handle);
}

However, I receive the following output currently:

$ php src/testFile.php 

@attr(Product) has the @attr(Category) from the @attr(Manufacturer). @attr(Product) has the @attr(Category) from the @attr(Manufacturer). @attr(Product) has the @attr(Category) from the @attr(Manufacturer). @attr(Product) has the @attr(Category) from the @attr(Manufacturer). @attr(Product) has the @attr(Category) from the @attr(Manufacturer). Array
(
    [0] => Product
    [1] => Category
    [2] => Manufacturer
    [3] => Price
    [4] => hasWlan
)
1
####################### 
@attr(Product) has the @attr(Category) from the @attr(Manufacturer). @attr(Product) has the @attr(Category) from the @attr(Manufacturer). @attr(Product) has the @attr(Category) from the @attr(Manufacturer). @attr(Product) has the @attr(Category) from the @attr(Manufacturer). @attr(Product) has the @attr(Category) from the @attr(Manufacturer). ####################### 
...

As you can see the data does not get replaced within my script.

Any suggestions why this is the case?

I appreciate your replies!

2
  • $singleVariable is array. Commented Jul 19, 2018 at 15:53
  • @u_mulder Thank you for your reply! Do I need another loop to make this code work? ;/ Commented Jul 19, 2018 at 15:54

1 Answer 1

1

As your $singleVariable is not single variable (rename it, don't confuse yourself and others), you should create an array of replacements from it. So, I think you should replace

$pattern = '@attr(' . $singleVariable . ')';

with:

$pattern = [];
foreach ($singleVariable as $variable) {
    $pattern[] = '@attr(' . $variable . ')'; 
}

Rest of the code of the function stays the same. As str_replace can work with arrays, everything should work. Also, you should fix your while loop to this:

while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
    // here you get headers, there's no need to increment `$row` any more than here
    if ($row == 0) {
        $vars = $data;
        $row++;
        //echo print_r($vars) . "\n";
    }
    // here you replace - array of `$vars` to array of `$data`
    echo $StringTemplate->process($template, $vars, $data);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.