0

I have the following string :

this is sample text
[abc def.pdf](https://example.com/post/abc def.pdf)
This is another line after file upload
[dfg.pdf](https://example.com/post/dfg.pdf)
![IMG_3261.JPG](https://example.com/post/IMG_3261.JPG)
this are some other line append to sting.

I want to convert this string in to as follow.

this is sample text
<a href="https://example.com/post/abc def.pdf">abc def.pdf</a>
This is another line after file upload
<a href="https://example.com/post/dfg.pdf">dfg.pdf</a>
<img src="https://example.com/post/IMG_3261.JPG"/ title="IMG_3261.JPG">
this are some other line append to sting.

How could I do it using laravel or php?

I know I can do that using php function preg_replace but I am not good at REGEXP. So please help me to do it.

what i try so far

i try this solution Why is my PHP regex that parses Markdown links broken?

But it will only converting to anchor tag but i also want to convert it to image tag too

preg_replace('@((https?://([-\w\.]+)+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?))@', '<a href="$1">$1</a>', $string); but it will not get output i mention above. How can i do it.

5
  • Consider to do the string conversion in a controller, and return the converted string to the view. Commented Jun 4, 2018 at 10:56
  • I am working with API so no need to send it in view. I had to do eveything in controller Commented Jun 4, 2018 at 10:59
  • You could use a markdown to html conversion library for this. That'll save you a lot of work. Commented Jun 4, 2018 at 11:03
  • I know this but i want to build something like github issue. Commented Jun 4, 2018 at 11:04
  • Please check both topics then modify your question if they weren't what you need. Commented Jun 4, 2018 at 11:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.