Sublime Forum

Automatic convert to php

#1

hi there,
is there a possibility to convert a html-code-line into a php-code-line automatically
like for instance:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
to
<?php
echo"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" >";
?>

Max

0 Likes

#2

Feel like a XY question. What are you actually going to do? There is no difference for the output in PHP in your example since everything outside the <?php ... ?> is auto echo-ed. The latter one you have to escape double quotes. Heredoc may be preferred for simplicity reason.

0 Likes

#3

sorry, I know that, but I’m absolute beginner with Sublime
so I do the question in another way,
example: I have copied a html-code from anywhere,
jump to my php-file, insert the code , and now I want to have the php-version of that inserted code with automatic masking of the quotation-marks, I’m too lazy to do that manually :innocent:
in reality I think I only mean the automatic masking of " to \" with one hit

0 Likes

#4

Create new plugin from Tools -> Developer -> New Plugin... and save the following code as PasteInPhpEchoCommand.py.

import sublime
import sublime_plugin


class PasteInPhpEchoCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        text_src = sublime.get_clipboard().strip("\r\n")

        if not text_src:
            return

        text_quoted = "echo <<<'HTML'\n{code}\nHTML;\n".format(code=text_src)

        self._doPaste(edit, text_quoted)

    def _doPaste(self, edit, text):
        for sel in self.view.sel():
            if len(sel) != 0:
                self.view.erase(edit, sel)

            self.view.insert(edit, sel.begin(), text)

Add a keybinding for the command:

{
  "keys": ["ctrl+alt+x"], // whatever keybinding you like
  "command": "paste_in_php_echo",
  "context": [
    // only work in PHP scope
    {
      "key": "selector",
      "operator": "equal",
      "operand": "embedding.php | source.php",
    },
  ],
}
0 Likes

#5

ok
and now?
I selected html-code in a php-file, pressed ctrl+alt+x
and ST3 made

echo <<<'HTML'

{
“keys”: [“ctrl+alt+x”], // whatever keybinding you like
“command”: “paste_in_php_echo”,
“context”: [
// only work in PHP scope
{
“key”: “selector”,
“operator”: “equal”,
“operand”: “embedding.php | source.php”,
},
],
}
HTML; from it
??

0 Likes

#6

next try:

test.php
<meta name="publisher" content="Max Herla">

ctrl+alt+x :

echo <<<'HTML'
<meta name="publisher" content="Max Herla">
HTML;
0 Likes

#7

ST3 takes the content of the clipboard and does it between

echo <<<'HTML'
```and 
HTML;
0 Likes

#8

You can also do this from a key binding alone without the plugin:

{
  "keys": ["ctrl+alt+x"], // whatever keybinding you like
  "command": "insert_snippet",
  "args": {
    "contents": "<?php\n\techo <<<'HTML'\n${0:$SELECTION}\nHTML;\n?>"
  },
  "context": [
    // only work in PHP scope
    { "key": "selector", "operator": "equal", "operand": "embedding.php | source.php" },
  ],
}

Yes, that was intended, because this method suggested by @jfcherng supports multi-line strings, where normal quotation would only be able to cover a single line. If you want that instead, replace the contents parameter with

"<?php\necho \"${0:$SELECTION}\";\n?>"
2 Likes

#9

In that case, double quotes, chars that already escaped and the $ sign (in double quotes) need special cares. Obviously insert_snippet can’t tackle it. Using heredoc is a much simpler solution imo.

1 Like

#10

But anyway, I believe there are enough information in this thread to achieve your custom goal.

0 Likes

#12

ok, very slowly I get an idea from what you’re speaking.
but this
[
{
“keys”: [“ctrl+alt+x”], // whatever keybinding you like
“command”: “insert_snippet”,
“args”: {
“contents”: “<?php\necho \"${0:$SELECTION}\";\n?>”
},

}
]

with this example
<meta name="robots" content="index, follow">
ends with this result
<?php
echo "<meta name="robots" content="index, follow">";
?>
nothing will be replaced.
what means ${0:$SELECTION} ?
I think, that doesn’t work
who selects the quotation marks ?

0 Likes

#13

sorry friends,
sorry for the mistake in my question, that produced a misunderstanding.
I realized, that I’ve forgotten to preformat my text in the beginning thread,
I did it now, please read it again.
Background of my question is, that I like to use emmet while coding. Emmet produces html-code with (if necessary) quotationmarks, these must be in the php-code ‘escaped’, so I need a solution to do that with ‘one click’ while coding html in php.
sorry for my mistake.
could you please think new ???:roll_eyes:
Max

0 Likes

#14

${0:$SELECTION} means the text of the selected region. If you hate to use heredoc, you may use single quotes to “reduce” the possibility that you have to deal with character escaping things because “command”: “insert_snippet”, only does plain paste, nothing more.

0 Likes

#15

ACTUALLY, you can apply regular expression replacements onto any variable in snippet expressions, including the $SELECTION variable, so…

"contents": "<?php\necho \"${0:${SELECTION/\"/\\\\\"/g}}\";\n?>"
2 Likes

#16

Thanks to FichteFoll,
it works, but I (still) don’t know why (very many backslashes) :smiley: .
Where can I get the commands and syntax for ST3 to do such things for myself ?

Max

by the way, when I copy the text from any thread here into my “keybinding-window” the quotation-marks are pasted wrong and force an error. I have to replace them with my keyboard.

0 Likes

#17

It’s a combined set of knowledge, but you’ll want to understand what snippets can do, how to create key bindings and the insert_snippet command.

The first set of escapes is for the JSON string. The second set, i.e. \\\\, is because we need a literal backslash in the regular expression replacement.

Sounds like you have some program running that affects your clipboard and replaces quotation marks with so-called smart quotes. Which OS are you on?

0 Likes

#18

Windows 10 PRO
Version 1809
Build 17763.973Unbenannt
the second keybinding is copied from here

thx for the links, but “Unofficial Documentation” ???

much to read :unamused:

0 Likes