Almost every language has a built-in function that can split a string at a given position. However, as soon as you have html tags in the string, the built-in function will not work properly.
Your task is to write a program or function which splits a string at the nth character but does not count characters of html tags and will output a valid html. The program must keep the formatting. Spaces outside the html tags may be counted or not counted, as you wish, but must be preserved. You can, however, exchange multiple consecutive spaces into a single space.
- the string
- the position to split at (0-based)
These can be taken as program or function arguments or can be read from the standard input.
Output: The split string which can be returned or written to the standard output.
The input will be valid html, it won't contain any entities (such as
). Tags that are opened after the character limit should be omitted from the output (see the last example).
<strong><i>more</i> <span style="color: red">complicated</span></strong>, 7
<strong><i>more</i> <span style="color: red">co</span></strong>
no html, 2
<b>no</b> <i>html root</i>, 5
<b>no img</b><img src="test.png" />more text, 6
You can use any language and the standard library of the given language. This is code golf, shortest program wins. Have fun!