Read anchor tag php from string

How to read the anchor tag(s) from a string and write the size of the file in bracket just after anchor tag completed in php

Perhaps you could show a sample of the string that you’re trying to analyse, and an example of what you want to extract from it. Also the code you’ve tried so far.

Thanks for your reply. Please find below details.

Sample Text

Lorem ipsum [dolor sit amet](file:///C:\Users\NICSI\Downloads\UE9QQUlSMTM5MDQ5NDc1_2022-02-28.pdf) , consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. [Ut enim ad minim](file:///C:\Users\NICSI\Downloads\report%20.xlsx) veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia dolor sit amet anim id est laborum.

Output

Lorem ipsum [dolor sit amet](file:///C:\Users\NICSI\Downloads\UE9QQUlSMTM5MDQ5NDc1_2022-02-28.pdf) (pdf – 50kb) , consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. [Ut enim ad minim](file:///C:\Users\NICSI\Downloads\report%20.xlsx) (xlsx – 12kb) veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla (docx – 19kb) pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia dolor sit amet (pdf – 50kb) , anim id est laborum.

The sample text consists of three file links and one hyperlink within the content. Now the output should be like the file link should show type of file with size of file as mentioned in the output

dolor sit amet – This is PDF file link

Ut enim ad minim – This is XLSX link

I am able to read all the links using below expression. But I am not sure how to write details after the links as mentioned in the output.

preg_match_all(‘~<a(.?)href=“([^”]+)"(.?)>~’, $text, $matches);

var_dump($matches[2]);

Sample Text

Lorem ipsum [dolor sit amet](file:///C:\Users\NICSI\Downloads\UE9QQUlSMTM5MDQ5NDc1_2022-02-28.pdf) , consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. [Ut enim ad minim](file:///C:\Users\NICSI\Downloads\report%20.xlsx) veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia dolor sit amet anim id est laborum.

Output

Lorem ipsum [dolor sit amet](file:///C:\Users\NICSI\Downloads\UE9QQUlSMTM5MDQ5NDc1_2022-02-28.pdf) (pdf – 50kb) , consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. [Ut enim ad minim](file:///C:\Users\NICSI\Downloads\report%20.xlsx) (xlsx – 12kb) veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla (docx – 19kb) pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia dolor sit amet (pdf – 50kb) , anim id est laborum.

The sample text consists of three file links and one hyperlink within the content. Now the output should be like the file link should show type of file with size of file as mentioned in the output

dolor sit amet – This is PDF file link

Ut enim ad minim – This is XLSX link

I am able to read all the links using below expression. But I am not sure how to write details after the links as mentioned in the output.

preg_match_all(‘~<a(.?)href=“([^”]+)"(.?)>~’, $text, $matches);

var_dump($matches[2]);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.