如何从长字符串(iframe)获取URL



如何在我的应用程序中从长字符串获取网址,我从服务器获取此字符串

"content" = "<p><iframe src="https://www.youtube.com/embed/HJ3EIbhwx2w" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"></iframe></p>n"

所以现在我想 https://www.youtube.com/embed/HJ3EIbhwx2w 出线。如何在 swift 中执行此操作?

理想情况下,您应该更改服务器以发送适当的信息。

否则,您可以使用正则表达式或NSScanner来查找 URL 并将其提取。此方法还可以更改宽度数字,而不是提取 URL。

你可以这样做

webView .loadHTMLString("<style>img,iframe{width: 100%;}</style>" + (postToDisplay?.postPostContent)!, baseURL: nil)

postToDisplay?.postPostContent是您实际的其他内容

好吧,

我终于通过 NSScanner 完成了它这是我的代码

func cutout(us:String)
{

    let scanner = NSScanner(string:us)
    var scanned: NSString?
    if scanner.scanUpToString("://", intoString:nil) {
        scanner.scanString("://", intoString:nil)
        if scanner.scanUpToString("width", intoString:&scanned) {
            let result: String = scanned as! String
            print("result: (result)")
            urlStringmy = result// result: google
            stringcutter()
        }
    }
}

最新更新