Home > Back-end >  Extract link from html in flutter
Extract link from html in flutter

Time:12-06

I am trying to extract a link from an HTML body which is coming from a response . Then message looks like this "content":

        {
        "rendered": 
         "<div style=
           "padding: 56.25% 0 0 0; 
            position: relative;">
              <iframe style=
                 "position: absolute; 
                  top: 0; 
                  left: 0; 
                  width: 100%; 
                  height: 100%;" 
                  title="Shrewsberry" 
                  src="https://player.vimeo.com/video/1000224?h=23334&amp;badge=0&amp;autopause=0&amp;player_id=0&amp;app_id=58479\" 
                  frameborder="0" 
                  allowfullscreen="allowfullscreen">
        </iframe>
        </div>"}

I want to get the content inside 'src="https://player.vimeo.com/video/1000224?' can anyone help .

This is my code which i use

final value = parse(html); // html is the value from response
String parsedString = parse(value.body!.text).documentElement!.text;
print(parsedString);

This is the results of what i got

CodePudding user response:

use this extension

extension StringExtension on String {
  String? extractVideoURL() {
    RegExp regExp = RegExp(
        r"src=(https:\/\/player.vimeo.com\/video\/[0-9] )",
        caseSensitive: false,
        multiLine: false);
    Iterable<RegExpMatch> matches = regExp.allMatches(this);
    if (matches.isNotEmpty) {
      return matches.first.group(1);
    }
    return null;
  }
}

CodePudding user response:

You can use this simple logic if your URL always ends with ?

void main(){ 
String x='"content": { "rendered": "<div style="padding: 56.25% 0 0 0; position: relative;"><iframe style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" title="Shrewsberry" **src="https://player.vimeo.com/video/1000224?**h=23334&badge=0&autopause=0&player_id=0&app_id=58479" frameborder="0" allowfullscreen="allowfullscreen">\n"}';
int start= x.indexOf('src');
int end= x.indexOf('?');
print(x.substring(start,end 1));
}

CodePudding user response:

You can use Regex to extract the string from your original string. Given the fact that it looks like HTML I ignored the ** you put in your answer.

In that case the regex (?<=src=").*?(?=") should work to retrieve the source. This will work for any source, not just Vimeo.

import 'dart:convert';
void main() {
  String input = "\"content\": { \"rendered\": \"<div style=\"padding: 56.25% 0 0 0; position: relative;\"><iframe style=\"position: absolute; top: 0; left: 0; width: 100%; height: 100%;\" title=\"Shrewsberry\" **src=\"https://player.vimeo.com/video/1000224?\"h=23334&badge=0&autopause=0&player_id=0&app_id=58479\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\">\n\"}";
  
  RegExp regex = new RegExp('(?<=src=").*?(?=")');
  var match = regex.firstMatch(input);
  
  if (match == null) {
    print("No match found.");
    return;
  }
  
  print("Result: "   match.group(0)!);
}

Output: Result: https://player.vimeo.com/video/1000224?

  • Related