Kamrujjaman Joy
Kamrujjaman Joy

Reputation: 113

Flutter: Fetch certain elements of a HTML TAG

I have a WordPress API that has a description and its HTML format. there are lots of different types of data like text image link H Tag and so on and also have a video URL. Now How I can get only the video portion from the description.

"content": {
"rendered": "\n<figure class=\"wp-block-video\"><video controls src=\"https://dev.6amtech.com/news/wp-content/uploads/2021/07/Forest-video-article.mp4\"></video></figure>\n\n\n\n<p>Its Just Test Video Article</p>\n",
"protected": false
}

Upvotes: 1

Views: 613

Answers (1)

Pablo
Pablo

Reputation: 6058

If the output is consistent then you can you extract the URL with simple string manipulation logic. Although some times you can't avoid using RegEx I prefer using something like this to extract the URL if this part of the HTML is going to be consistent.

String html = "\n<figure class=\"wp-block-video\"><video controls src=\"https://dev.6amtech.com/news/wp-content/uploads/2021/07/Forest-video-article.mp4\"></video></figure>\n\n\n\n<p>Its Just Test Video Article</p>\n";
String urlPrefix = '<video controls src="';
  
int urlStart = html.indexOf(urlPrefix) + urlPrefix.length;
int urlEnd   = html.indexOf('"', urlStart);  
  
print(html.substring(urlStart, urlEnd));
// https://dev.6amtech.com/news/wp-content/uploads/2021/07/Forest-video-article.mp4

Upvotes: 2

Related Questions