Sunday 24 January 2021

How to get Image URL from a RichTextArea field

 Hi,

Here we are going to learn how to get an Image URL from a RichTextArea if someone adds an image in RichTextArea.

String decodedImageUrl;

 Matcher imgMatcher = Pattern.compile( '<img(.+?)>' ).matcher(<RichTextArea_FieldAPIName>);

// iterate each image tag found

        while ( imgMatcher.find() ) {        

            // get the image tag html

            String imageTag = imgMatcher.group();

            System.debug( 'imageTag=' + imageTag );

            String imageURL = imageTag.substringBetween( ' src="', '"' );

            System.debug( 'imageURL=' + imageURL );

            decodedImageUrl= imageURL.unescapeHtml4();

         }  

Reference:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_pattern_and_matcher_matcher_methods.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_pattern_and_matcher_pattern_methods.htm#apex_classes_pattern_and_matcher_pattern_methods


No comments:

Post a Comment

Best Practices for Building Prompt Templates

 Hi, The following best practices should be considered when building prompt templates: Make sure that your prompt templates are concise and ...