首页 > 解决方案 > 使用 Laravel 或 PHP 将 sting 转换为 html 代码

问题描述

我有以下字符串:

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. 所以请帮我做。

到目前为止我尝试了什么

我尝试这个解决方案 为什么我解析 Markdown 链接的 PHP 正则表达式被破坏了?

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

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

标签: phpregexlaravel

解决方案


推荐阅读