-1

I'm having some problems to get a text in a specific format into real working PHP code.

My text file:

@T1:The German sociologist Max Weber once proposed
@S:Jos Bleau
@C:jos.bleau@domain.com
@L:"He used to be so conservative," she says, throwing up her hands in mock exasperation. "We used to have the worst arguments right here at this table. I was part of the first group of public city school teachers that struck to form a union, and Richard was very angry with me. He saw unions as corrupt. He was also very opposed to social security. He thought people could make much more money investing it on their own. Who knew that within 10 years he would become so idealistic
@R:At first, <@Ri>Stallman viewed these notices<@$p> with alarm. Rare was the software program that didn't borrow source code from past programs, and yet, with a single stroke of the president's pen, Congress had given programmers and companies the power to assert individual authorship over communally built programs. It also injected a dose of formality into what had otherwise been an informal system.

The AI Lab of the 1970s was by all accounts a special place. Cutting-edge projects and top-flight researchers gave it an esteemed position in the world of computer science. The internal hacker culture and its anarchic policies lent a rebellious mystique as well. Only later, when many of the lab's scientists and software superstars had departed, would hackers fully realize the unique and ephemeral world they had once inhabited.

As a single parent for nearly a decade-she and Richard's father, Daniel Stallman, were married in 1948, divorced in 1958, and split custody of their son afterwards-Lippman can attest to her son's aversion to authority. She can also attest to her son's lust for knowledge. It was during the times when the two forces intertwined, Lippman says, that she and her son experienced their biggest battles.
@ST:Fusions
@R:Such mythological descriptions, while extreme, underline an important fact. The ninth floor of 545 Tech Square was more than a workplace for many. For hackers such as Stallman, it was home.

The belief in individual freedom over arbitrary authority extended to school as well. Two years ahead of his classmates by age 11, Stallman endured all the usual frustrations of a gifted public-school student. It wasn't long after the puzzle incident that his mother attended the first in what would become a long string of parent-teacher conferences.
@ST:Fusions
@R:The belief in individual freedom over arbitrary authority extended to school as well. Two years ahead of his classmates by age 11, Stallman endured all the usual frustrations of a gifted public-school student. It wasn't long after the puzzle incident that his mother attended the first in what would become a long string of parent-teacher conferences.

@BV:Thirty years later, Breidbart remembers
@CP:(Picture: Credit – Jos Bleau) or @CP:(Picture: Thanks)

The expected output I need (Half pseudo code; Unescaped quotes):

<?php

$title1 = 'The German sociologist Max Weber once proposed';
$signature = 'Jos Bleau';
$email = 'jos.bleau@domain.com';
$lead = '"He used to be so conservative," she says, throwing up her hands in mock exasperation. "We used to have the worst arguments right here at this table. I was part of the first group of public city school teachers that struck to form a union, and Richard was very angry with me. He saw unions as corrupt. He was also very opposed to social security. He thought people could make much more money investing it on their own. Who knew that within 10 years he would become so idealistic';   
$text[] = 'At first, <@Ri>Stallman viewed these notices<@$p> with alarm. Rare was the software program that didn't borrow source code from past programs, and yet, with a single stroke of the president's pen, Congress had given programmers and companies the power to assert individual authorship over communally built programs. It also injected a dose of formality into what had otherwise been an informal system.

The AI Lab of the 1970s was by all accounts a special place. Cutting-edge projects and top-flight researchers gave it an esteemed position in the world of computer science. The internal hacker culture and its anarchic policies lent a rebellious mystique as well. Only later, when many of the lab's scientists and software superstars had departed, would hackers fully realize the unique and ephemeral world they had once inhabited.

As a single parent for nearly a decade-she and Richard's father, Daniel Stallman, were married in 1948, divorced in 1958, and split custody of their son afterwards-Lippman can attest to her son's aversion to authority. She can also attest to her son's lust for knowledge. It was during the times when the two forces intertwined, Lippman says, that she and her son experienced their biggest battles.'; 
$subtitle[] = 'Fusions';
//etc...
?>

Note:

  • The names like $title1 and @T1 are completely unrelated to each other and $title1 is just used as example. It could also be $xy or something else

  • If @XY appears more than once in the file then the values should be added as array element, else as simple assignment

I don't know if preg_split() is the correct direction and I can do it with it? Or do I have to use other functions to accomplish this?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • How do you come from e.g. `@ST` to `$subtitle`? Do you have an array or something like that to look that up? Also how do you choose between an assignment and adding the value to an array? `$title1 = ...` / `$subtitle[] = ...` – Rizier123 Mar 25 '16 at 19:45
  • @Rizier123 That the point of my question... I need to convert this text into PHP code... :-) – Alfred Dagenais Mar 25 '16 at 19:48
  • Yes I see that, but how do you translate: `@ST` to `$subtitle`? It could also be `@ST` to `$st`. Where is the relation between `@ST` and `$subtitle`? Also how do you know if you want to assign a string or just add it as array value? – Rizier123 Mar 25 '16 at 19:56
  • @Rizier123 haaa yes you have right... I don't have relation etablish with the `@ST` and `$subtitle` for the moment... It's just an example what I want. But yes if you prefer the target php sting can be `$st` – Alfred Dagenais Mar 25 '16 at 20:06
  • Just saw the pattern. If you have more than one of `@X:` then you want an array. – Rizier123 Mar 25 '16 at 20:16

1 Answers1

0

Explanation

First we get the data from the text file into a variable with file_get_contents() and also initialize our $output array, where each element is a line in the output, with a php tag <?php.

You can also modify $lookup with shortcut => variable name elements, where you can define which @XY: gets replaced with which variable name. If not defined the shortcut will be used as variable name.

Now that we have prepared some stuff we match each @XY: with the corresponding data with preg_match_all().

Regular Expression

/@(\w+):(.*?)(?=@\w+:)/s
  • (.*?) matches everything as much as needed
    • With the flag s, * also matches new lines
  • (?=@\w+:) makes sure (.*?) matches everything until the next @XY: and not more. Where ?= is a positive lookahead and as it says it looks ahead if that regex in the parentheses(@\w+) can be matched

We also preemptively save the amount each shortcut appears in the data with array_count_values().

Now that we have matched all data which we want we can loop through all shortcuts, which are saved in $m[1]. In the foreach loop we simply check if you have defined a lookup variable name or if we use the shortcut as variable name.

Then we simply add each assignment as new element to the output array. Where you have to note three things:

  • Complex (curly) syntax is used, so that you don't get problems with invalid variable names, see: How can I access a property with an invalid name?

  • Depending on how many times a shortcut appeared in the data we decide if it should be added as array element or normal assignment. If the shortcut appears more than once in the data it will be adding the value as array element else as simple string assignment

  • We use trim() to remove spaces, new lines, ... from the start and end of the string. And we use addslashes(), so we don't get problems with quotes

Done. And now we are already done. Just depending on how you want to output the result you can save it to a file with file_put_contents() or just print out the array.

Code

<?php

    $text = file_get_contents("test.txt");
    $output = ["<?php"];
    $lookup = [];  //Example: ["ST" => "subtitle"]

    preg_match_all("/@(\w+):(.*?)(?=@\w+:)/s", $text, $m);
    $variableShortcutCount = array_count_values($m[1]);

    foreach($m[1] as $key => $variableShortcut){

        if(isset($lookup[$variableShortcut])){
            $output[] = '${"' . $lookup[$variableShortcut] . ($variableShortcutCount[$variableShortcut] > 1 ? '"}[]' : '"}') . " = '". addslashes(trim($m[2][$key])) . "';" ;
        } else {
            $output[] = '${"' . $variableShortcut . ($variableShortcutCount[$variableShortcut] > 1 ? '"}[]' : '"}') . " = '". addslashes(trim($m[2][$key])) . "';" ;
        }

    }

    //Output to file
    //file_put_contents("output.txt", implode(PHP_EOL, $output));

    //Output to browser
    echo "<pre><code>";
    highlight_string(implode(PHP_EOL, $output));

?>

output:

<?php
${"T1"} = 'The German sociologist Max Weber once proposed';
${"S"} = 'Jos Bleau';
${"C"} = 'jos.bleau@domain.com';
${"L"} = '\"He used to be so conservative,\" she says, throwing up her hands in mock exasperation. \"We used to have the worst arguments right here at this table. I was part of the first group of public city school teachers that struck to form a union, and Richard was very angry with me. He saw unions as corrupt. He was also very opposed to social security. He thought people could make much more money investing it on their own. Who knew that within 10 years he would become so idealistic';
${"R"}[] = 'At first, <@Ri>Stallman viewed these notices<@$p> with alarm. Rare was the software program that didn\'t borrow source code from past programs, and yet, with a single stroke of the president\'s pen, Congress had given programmers and companies the power to assert individual authorship over communally built programs. It also injected a dose of formality into what had otherwise been an informal system.

The AI Lab of the 1970s was by all accounts a special place. Cutting-edge projects and top-flight researchers gave it an esteemed position in the world of computer science. The internal hacker culture and its anarchic policies lent a rebellious mystique as well. Only later, when many of the lab\'s scientists and software superstars had departed, would hackers fully realize the unique and ephemeral world they had once inhabited.

As a single parent for nearly a decade-she and Richard\'s father, Daniel Stallman, were married in 1948, divorced in 1958, and split custody of their son afterwards-Lippman can attest to her son\'s aversion to authority. She can also attest to her son\'s lust for knowledge. It was during the times when the two forces intertwined, Lippman says, that she and her son experienced their biggest battles.';
${"subtitle"}[] = 'Fusions';
${"R"}[] = 'Such mythological descriptions, while extreme, underline an important fact. The ninth floor of 545 Tech Square was more than a workplace for many. For hackers such as Stallman, it was home.

The belief in individual freedom over arbitrary authority extended to school as well. Two years ahead of his classmates by age 11, Stallman endured all the usual frustrations of a gifted public-school student. It wasn\'t long after the puzzle incident that his mother attended the first in what would become a long string of parent-teacher conferences.';
${"subtitle"}[] = 'Fusions';
${"R"}[] = 'The belief in individual freedom over arbitrary authority extended to school as well. Two years ahead of his classmates by age 11, Stallman endured all the usual frustrations of a gifted public-school student. It wasn\'t long after the puzzle incident that his mother attended the first in what would become a long string of parent-teacher conferences.';
${"BV"} = 'Thirty years later, Breidbart remembers';
${"CP"} = '(Picture: Credit – Jos Bleau) or';
Community
  • 1
  • 1
Rizier123
  • 58,877
  • 16
  • 101
  • 156