将image.jpg转换为<img src="image.jpg"/>

时间:2011-04-11 14:47:27

标签: php

我看到这是Facebook,当用户粘贴图片链接时 www.blahblah.blah \ blah.jpg 它自动进入<img src="...."/> 怎么用PHP做?

3 个答案:

答案 0 :(得分:1)

您正在寻找类似BBCode的替换系统。互联网上有很多这样的网络。一个例子:http://snipplr.com/view/2296/php--bbcode/

答案 1 :(得分:0)

我不是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

答案 2 :(得分:0)

PHP是服务端,因此您需要使用像Javascript这样的客户端技术。

更新

但您可以将内容发送到服务器并让PHP解析和格式化。但这取决于你,因为我不知道你是否使用Ajax或者你想提交这个吗?

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

我自己没有实现这一点,所以一定要测试,因为我不知道textchange将如何通过粘贴等操作执行。有关其他活动的建议吗?

相关问题