Skip to content

Instantly share code, notes, and snippets.

View tom00's full-sized avatar

Tomasz Gajewski tom00

View GitHub Profile
@tom00
tom00 / gist:67a925c9fc6cfc659efaf023fe9f4f38
Last active March 12, 2018 16:43
Truncate string containing HTML tags
It's given a sample string.
s = "<p class="paragraph">Turnip greens yarrow ricebean rutabaga endive cauliflower sea lettuce kohlrabi amaranth water <a href="https://www.google.pl/search?q=spinach" class="link">spinach</a> avocado daikon Süßkartoffel napa cabbage <strong>asparagus winter purslane kale. Celery potato scallion desert</strong> raisin horseradish spinach carrot soko. Lotus root water spinach fennel kombu maize <span style="font-size: 19px;color: blue;">bamboo shoot green bean swiss chard seakale pumpkin onion chickpea gram corn pea.</span> Brussels sprout coriander water chestnut gourd swiss chard wakame kohlrabi beetroot carrot watercress. Corn amaranth salsify bunya nuts nori azuki bean chickweed potato bell pepper artichoke.</p>"
The string is UTF-8 encoded.
Develop a function to truncate the string and add ellipsis to its end.
Truncated string shall not to exceed n characters including ellipsis under the following conditions:
- do not break a word apart;
- do not break a html element apart
apiVersion: v1
kind: Service
metadata:
name: db
labels:
app: db
k8s-app: magento
spec:
selector:
app: db