0

How to replace string in kotlin? I have example here with html string.

var test = "<html><body><p> just some test text</p> And i wanna use this text for texting and i ll show you this image https://www.instagram.com/p/B8I9_KiF45g/?utm_source=ig_web_copy_link also this is the instagram photo with just normal link id body https://www.instagram.com/p/B8I3r66pVpp/ <h> random title text </h></body></html>"

Is there an option to add only instagram links in < iframe> tags, add 'embed' string at the end of the link, so output would be like this:

"<html><body><p> just some test text</p> And i wanna use this text for texting and i ll show you this image <iframe src="https://www.instagram.com/p/B8I9_KiF45g/embed"> </iframe> also this is the instagram photo with just normal link id body <iframe src="https://www.instagram.com/p/B8I3r66pVpp/embed" </iframe> <h> random title text </h></body></html>"
artois
  • 485
  • 1
  • 6
  • 13

2 Answers2

1

I would parser the html string with specific library like DOMParser-kotlin and then loop for the iframes and modify the src attributes

Teku
  • 35
  • 8
  • But I do not have iframes, need to add them also. – artois Feb 04 '20 at 11:17
  • Oh yes, I see. Maybe you can split the string by spaces and search for the substrings that starts by "https" and concatenate ifram tag at the beginning and at the end. And with this substring tagged replace the indexOf('?') to length by embed. Take care with the special characters as the question comment said – Teku Feb 04 '20 at 13:23
1

This is not perfect (as regex matching HTML has some issues) but it might work for your specific use case and should at least get you started. It uses a combination of RegEx and replace (bundled in an extension function) to find links and embed them:

fun String.embedded(): String {
    // Match everything starting with https://www.instagram.com/ until the first question mark, whitespace or new tag.
    // The second group is used to get rid of any query parameters.
    val linkRegex = "(https://www.instagram.com/[^\\? <]*)(\\?[^ <]*)?".toRegex()
    // $1 is the first matching group, i.e. the link itself.
    val output = this.replace(linkRegex, """<iframe src="$1embed"> </iframe>""")
    return output
}

Short sample for your provided data:

fun main() {
    val input = "<html><body><p> just some test text</p> And i wanna use this text for texting and i ll show you this image https://www.instagram.com/p/B8I9_KiF45g/?utm_source=ig_web_copy_link also this is the instagram photo with just normal link id body https://www.instagram.com/p/B8I3r66pVpp/ <h> random title text </h></body></html>"
    val expected = """<html><body><p> just some test text</p> And i wanna use this text for texting and i ll show you this image <iframe src="https://www.instagram.com/p/B8I9_KiF45g/embed"> </iframe> also this is the instagram photo with just normal link id body <iframe src="https://www.instagram.com/p/B8I3r66pVpp/embed"> </iframe> <h> random title text </h></body></html>"""

    val output = input.embedded()
    println(output)
    println(output == expected)
}
Marvin
  • 13,325
  • 3
  • 51
  • 57