php - 使用 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);
但它不会得到我上面提到的输出。我该怎么做。
解决方案
推荐阅读
- java - 如何将单词存储为 TreeMap 中的键?
- python - 或者工具 cp_model 知道哪个约束失败了
- typescript - 更有用的打字稿字典类型:警告访问的项目可能未定义
- angular - 如何合并多个 Observable 响应
- python - 是否可以在 spaCy IOB 转换器生成的 JSON 中检索整个句子?
- google-analytics - 如何在我的 Google 网站上发布 Google Analytics 访问者地图?
- typescript - 打字稿上的 Socket.io 附加属性不存在
- python - 如何在 Python 3.+ 中将所有数据从 VirtualEnv 移动到 CondaEnv
- linux - 使用 sed 命令在 linux 中列出以特定字符开头的元素
- python - 如何从合并的数据框中过滤记录