Good night everybody.

There is such html code:

<div class="test" title="Подсказка"><span><img src="/images.png" alt="картинка"></span><span>Текст</span></div> 

There are about 800 such lines (all of them are of the same type); only the "Hint", "/images.png" and "Text" changes.

Now there is a need to change the structure to this:

 <div class="test"><span>#1</span><span><img src="/images.png" alt="картинка"></span><span>Текст</span><span>Подсказка</span></div> 

Is it possible to do this on the machine? or have to redo everything handles?

  • Yes, you can, just write a script and drive through it your 800 lines. He parsit and insert what is needed and where necessary. Php and where is unknown, perhaps due to the fact that something is unsaid. - VostokSisters
  • 2
    Yes, it seems like it can be in a regular notepad ++ search-replace regular - Alex Shimansky

2 answers 2

I understand that #1 is an iterative element, i.e. for it it is necessary to do +1 in each subsequent case.

I wrote you a script, where a certain file file.html (you can change the file name yourself) is reduced to the structure you need and saved to fileNew.html . In order to run the script, you need to have PCP installed, then you need to place this script (for example, create a file php.php) in the folder containing this file.html and run it (for example, call Type: http://ваш_сайт.ру/папка_где_лежит_хтмл/php.php .

 <?php $file = __DIR__.'/file.html'; $text = file_get_contents($file); $pattern = '/<div class="test" title="([^"]+)"><span>(<img [^>]+>)<\/span><span>(.*?)<\/span><\/div>/imsu'; $_GET['i'] = 0; $textNew = preg_replace_callback($pattern, 'myReplace', $text); function myReplace($matches) { $_GET['i']++; return '<div class="test"><span>#'.$_GET['i'].'</span><span>'.$matches[2].'</span><span>'.$matches[3].'</span><span>'.$matches[1].'</span></div>'; } $fileNew = __DIR__.'/fileNew.html'; file_put_contents($fileNew, $textNew); 
  • Thank you very much. Problem solved - Eva_m

In general, there are two options for how you can try to do this, and both of them will depend on which editor you use. I would advise using either notepad ++ or PHPStorm, in the second case it is no longer just a text editor, but a full-fledged IDE (Integrated Development Environment). Or any other editor supporting text replacement and / or multi-cursor.

The first option is to use text replacement, for example, in notepad ++, it is called by Ctrl + H, and you can replace all the blocks in the code one by one, for example, you can change the <div class="test" title="Подсказка"><span> to <div class="test"><span> .

But in this embodiment, you will not be able to do numbering (digits after #) automatically, for example, just make a new code structure.

The second option : use a multi-cursor. You can read more about it in the case of PHPStorm here , and in the case of np ++ it is done like this:

First of all, this function needs to be activated, in the Russian version it is done along this path: Options / Settings / Edit / Multi-editing. Further, using either the text replacement function or the multicursor function itself, you modify the structure of the code, in your case I would advise using the replacement. And then do the numbering as follows. Select that piece of text, after which the increasing number should appear and press Alt + C, as a result you will see a window with two options for work, either “text to insert” or “numbers to insert”. You will need to configure everything like this:

step 1

Please note that the numbers will be inserted where you have the cursor during text selection. Then click OK and get the following text:

enter image description here

This is if you left the cursor to the right of #. If you leave the cursor on the left side, you will get this picture, do not mess up:

enter image description here

  • Thank. For another case, line numbering helped - Eva_m