-1

enter image description hereso i have this format of the text in below and i need to replace the first CR LF tabulation with space :

Contenu
    2 encarts
    12 encarts

Prepresse
    Fichier fourni

and i want the result:

Contenu 2 encarts
        12 encarts

Prepresse Fichier fourni
blood
  • 35
  • 3

2 Answers2

0

can you give more info regarding the source of this text you want to format? it seems like Contenu and Prepresse Fichier are both names for a group

where you have items like 2 encarts , 12 encarts and fourni as items in those groups

The first thing is to detect if the text is a group name or an item, I hope this can be detected from the source where your text comes from. The second thing is echoing the items correctly.

EDIT:

I do a lot using arrays, created your output using this code:

$text = "Contenu\n\t2 encarts\n\t12 encarts\n\nPrepresse\n\tFichier fourni";

//divide the groups
$groups = explode("\n\n", $text);

//loop groups
foreach ($groups as $group) {

    //get group name and items
    $items = explode("\n\t", $group);

    //loop items, 
    foreach ($items as $item => $value) {
        switch ($item) {
            case 0: 
                //first item is group name
                echo $value . " ";
                //get the length of this group name to align all items using spaces
                $length = strlen($value) + 1;
                break;
            case 1: //second item is first value, what apears next to the group name
                echo $value . "<br>";
                break;
            default: // other items, where the spaces are the length of the groupname
                echo str_repeat("&nbsp;", $length) . $value . "<br>";
                break;
        }
    }

    //when an entire group is shown, leave an empty space
    echo "<br>";

}

?>

shows me this as output:

Contenu 2 encarts
        12 encarts

Prepresse Fichier fourni

hope this helps

haypro
  • 85
  • 13
  • i var_dumped i have string contains that but how can i detected the group and item i found the CR LF and Tabulation can your help me @haypro – blood Dec 08 '15 at 15:25
0

Using regexps can be done as:

<?php
$text = "Contenu\n\t2 encarts\n\t12 encarts\n\nPrepresse\n\tFichier fourni";    
echo $text."\n";    
echo preg_replace('/((^.+)(\n\t))/ime', "str_replace('$3', ' ', '$0')", $text);
?>

output of this is:

Contenu
    2 encarts
    12 encarts

Prepresse
    Fichier fourni
Contenu 2 encarts
    12 encarts

Prepresse Fichier fourni
xmike
  • 1,029
  • 9
  • 14
  • i used that for this string "Contenu 2 encarts 12 encarts Prepresse Fichier fourni"but nothing do thanks in advance @xmike – blood Dec 08 '15 at 16:03
  • well, if the string is just as you put above -- there are no newline and tabulation characters in it, so nothing happened. Please compare to `$text = "Contenu\n\t2 encarts\n\t12 encarts\n\nPrepresse\n\tFichier fourni";`. I've put it in the sapmle by intention to show newlines and tabulation explicitely. Newlines and tabs create some formatting that is used to lookup for the correct places. – xmike Dec 08 '15 at 16:08
  • and I also have to mention that my sample is php-injection insecure. In practice `preg_replace_callback` function should be used. – xmike Dec 08 '15 at 16:11
  • so this string contains CRLF and -----> tabulation open in notpad++ and you the CRLF and --------> tabulation i tested but nothing give me – blood Dec 08 '15 at 16:23
  • hmm... the sample code provided and its output is the real code and real output, I just copy-pasted it from IDE. Did you try it as is is? – xmike Dec 08 '15 at 16:33
  • no i try with my string if you have the @mail i sent to you file notepad++ to see my string – blood Dec 08 '15 at 16:38
  • For someone else could also help I think it would be better just to attach to the question a screenshot of your file in Notepad++ with _View -> Show Symbol -> Show All Characters_ option enabled. – xmike Dec 08 '15 at 17:35
  • `preg_replace('/(^.+)(\r\n\t)/ime', "str_replace('$2', ' ', '$0')", $text);` worked for me ))) It really was different from comparing your file and `"Contenu\n\t2 encarts\n\t12 encarts\n\nPrepresse\n\tFichier fourni"`. File has CR-s – xmike Dec 08 '15 at 18:20
  • i tetsed that preg_replace('/(^.+)(\r\n\t)/ime', "str_replace('$2', ' ', '$0')", $text);with my string but not worked for me $ text the text of the file – blood Dec 09 '15 at 09:16