V
V
Valeriu Vodnicear2019-11-09 21:55:15
PHP
Valeriu Vodnicear, 2019-11-09 21:55:15

How to find and replace code in markdown line?

How to extract code from markdown text?
in the text, the code can occur several times,
right now I wrote the following algorithm:

  • markdown text convert to html using this package
  • find tags in html"<pre><code >"
  • extract all text up to closing tags

this is the first part of the program, simpler, then more difficult:
you need to find and replace this piece of code in markdown with a special label, like this: {это метка 1}
I will give an example, there is text:
Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.
```
var ipsum = "123";
var ipsum2 = '321';
```
Lorem ipsum is placeholder text.
```
var ipsum3 = "456";
var ipsum4 = '654';
```
Don't!

Now we need to get both pieces of code in a separate array and replace them in markdown with special labels.
I can give my code as an example, but it is quite large, and I think that the direction I have chosen is not correct. Project on Laravel.
It is better to find the code in Mardkdown right away than to use html for this purpose.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2019-11-10
@dodo512

'/^```\w*\n(.*?)^```/ms'
preg_match_all is just a search, and replacement is preg_replace or preg_replace_callback.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question