Php 将image.jpg转换为<;img src=”等;image.jpg“&燃气轮机;

Php 将image.jpg转换为<;img src=”等;image.jpg“&燃气轮机;,php,Php,我见过这是Facebook,当用户粘贴图片链接时 www.blahblah.blah\blah.jpg 它会自动进入 如何在PHP中实现这一点?我不是PHP程序员,但问题似乎很简单。总体思路(非特定语言形式)是: 1. Get user input somewhere into a string (in facebook - post on a wall or some other place) 2. Search the string for the image extension (.j

我见过这是Facebook,当用户粘贴图片链接时 www.blahblah.blah\blah.jpg 它会自动进入

如何在PHP中实现这一点?

我不是PHP程序员,但问题似乎很简单。总体思路(非特定语言形式)是:

1.  Get user input somewhere into a string (in facebook - post on a wall or some other place)
2.  Search the string for the image extension (.jpg, .png, .gif, etc.)
3.  If the image extension is found, append the tags to either side of the string appropriately
4.  Act further on the event of the image extension being found

您正在寻找一种类似于替换系统的BBCode。网上有很多这样的人。例如:

PHP是服务端,因此您需要使用客户端技术,例如Javascript

更新:

但是您可以将内容发送到服务器,让PHP解析并格式化。但这取决于您,因为我不知道您是使用Ajax还是希望在提交时使用它?

您可以使用正则表达式和Javascript识别任何链接,这将侦听textchange。Javascript将在每次更改文本时扫描文本,然后您可以将所有链接、电子邮件等转换为链接而不是文本。这将为您的用户提供良好的效果


我自己还没有实现这个功能,所以一定要进行测试,因为我不知道textchange在粘贴等操作中的性能如何。对其他活动有什么建议吗?

请注意处理用户选择的图像时会遇到的各种问题,例如大小调整、页面放大、文件加载过大等。可能会重复nope,但这不是该事件的重复!