使用Laravel或PHP将sting转换为html代码

时间:2018-06-04 10:51:54

标签: php regex laravel

我有以下字符串:

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.

我想将此字符串转换为如下。

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.

我怎么能用laravel或php来做呢?

我知道我可以使用php函数preg_replace来做到这一点,但我不擅长REGEXP。所以请帮我做。

到目前为止我尝试了什么

我尝试这个解决方案 Why is my PHP regex that parses Markdown links broken?

但它只会转换为锚标记,但我也想将其转换为图像标记

preg_replace('@((https?://([-\w\.]+)+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?))@', '<a href="$1">$1</a>', $string); 但我上面提到它不会得到输出。我怎么能这样做。

0 个答案:

没有答案