Question

I have a csv file which I need to import on a php-based system - Magento to be specific. There is a field which contains the description of a product and is whitespace formatted for indentation as well as dashes (-) used for list items. I could use a <pre> tag and display it as it is, but when I got the csv file, somehow the lines which could fit in a single line had already moved to another line. So there is no real success trying to replace \r\n with <br/> (or using the nl2br function) because I do not want something that can fit in one line to be displayed in two lines.

I am not sure if this can be achieved with php, VBA routine in Excel or some Magento extension (I am using MAGMI at the moment and it does not have such facilities). So I ask this question with an open option of offering a solution with either of those.

Anyone has a tip for this please?

EDIT::

Should look like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

  • Praesent cursus eu eros quis laoreet.
  • In tincidunt massa sed dui aliquam placerat.

Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam a nulla id dui semper tristique. Vivamus rutrum auctor neque, eu tincidunt magna dapibus vitae. Integer felis mi, luctus ut mollis at, mollis nec lacus. Vestibulum et dictum turpis. Praesent in neque sed mauris semper hendrerit.

  • Vivamus rhoncus magna ipsum
  • sit amet ullamcorper lectus eleifend ut. Sed semper dui quis accumsan suscipit.
  • Donec eu lacus sed dolor fermentum fermentum.
  • Curabitur iaculis molestie ante a bibendum.

Which means:

  • Text should not contain a line break unless it is intended to have one.
  • Width of the text should depend on the container rather than whitespace formatting (somehow the text is narrow and does not extend even if I adjust the column width on csv)#
  • If possible, dashes should be replaced with a list item enclosure, but that is not the biggest problem at this moment.

Text currently looks like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. - Praesent cursus eu eros quis laoreet. - In tincidunt assa sed dui aliquam placerat. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam a nulla id dui semper tristique. Vivamus rutrum auctor neque, eu tincidunt magna dapibus vitae. Integer felis mi, luctus ut mollis at, mollis nec lacus. Vestibulum et dictum turpis. Praesent in neque sed mauris semper hendrerit. - Vivamus rhoncus magna ipsum - sit amet ullamcorper lectus eleifend ut. Sed semper dui quis accumsan suscipit. - Donec eu lacus sed dolor fermentum fermentum. - Curabitur iaculis molestie ante a bibendum.

*And if I use <pre> tag, it looks like this *

Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. 
- Praesent cursus eu eros 
quis laoreet. 
- In tincidunt massa sed dui 
aliquam placerat. 

Interdum et malesuada fames 
ac ante ipsum primis in faucibus. 
Aliquam a nulla id dui semper 
tristique. Vivamus rutrum auctor 
neque, eu tincidunt magna 
dapibus vitae. Integer felis mi, 
luctus ut mollis at, mollis nec 
lacus. Vestibulum et dictum turpis. 
Praesent in neque sed mauris 
semper hendrerit. 

- Vivamus rhoncus magna ipsum 
- sit amet ullamcorper lectus 
eleifend ut. Sed semper dui 
quis accumsan suscipit. 
- Donec eu lacus sed dolor 
fermentum fermentum. 
- Curabitur iaculis molestie ante 
a bibendum.

So my product description looks either as a single unformatted block (which I understand because there is no html tags in there) or if I try preserving the whitespace formatting, it looks too narrow (does not cover the whole width of the container).

Hope this is detailed enough.

P.S.::

Its just like how a ckeditor would allow users to type in text, press enter and at the back of it, ckeditor would replace them with paragraph tags etc. May be this operates on the back of a regex that can be referred to? Anyone know about that?

Was it helpful?

Solution

What you're looking for is a Markdown to HTML converter. Magmi doesn't have a plugin that can do this, however you could easily write one yourself.

  1. Create a Magmi plugin folder and file.
  2. Include a Markdown to HTML PHP library, such as this: https://github.com/michelf/php-markdown
  3. Use the processItemBeforeId() function to update the descriptions on the fly in the plugin, like this:

    public function processItemBeforeId(&$item,$params=null)
    {
        //Use the Markdown class library to convert the description.
        $updated_description = Markdown::defaultTransform($item['description']);
    
        //Update the description of the item before it gets imported.
        $item['description'] = $updated_description;
    }
    
  4. Enable your custom Magmi plugin, and it should start converting the descriptions on the fly when you import.

OTHER TIPS

Have you try to add html tags using basic regular expression replacement ? (Using preg_replace to catch and replace list elements by their appropriate tags for example)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top