检测图像URL并包装IMG标签?

时间:2018-07-29 14:41:36

标签: php wordpress buddypress

尝试破解Wordpress安装,以在特定插件的注释部分添加更多的用户友好性。

给出三种情况:

1

#!/bin/bash

echo "Updating the system, this might take a couple minutes..."
sudo yum -y update

echo "Installing Docker"
curl -fsSL https://get.docker.com/ | sh

echo "Starting Services"
sudo systemctl start docker
sudo systemctl enable docker

echo "Creating Network..."
sudo docker network create mongo-cluster

echo "Creating 3 MongoDB Instances"

sudo docker run -p 30001:27017 --name primary --net mongo-cluster mongo 
mongod --replSet comments && sudo docker start primary

sudo docker run -p 30002:27017 --name failover1 --net mongo-cluster mongo 
mongod --replSet comments && sudo docker start failover1

sudo docker run -p 30003:27017 --name failover2 --net mongo-cluster mongo 
mongod --replSet comments && sudo docker start failover2

docker exec -it primary bash -c "echo 'db = (new 
Mongo(\"localhost:27017\")).getDB(\"test\")' | mongo && echo 'config = 
{\"_id\": \"comments\",\"members\": [{\"_id\": 0,\"host\": 
\"primary:27017\"},{\"_id\": 1,\"host\":\"failover1:27017\"},{\"_id\": 
2,\"host\": \"failover2:27017\"}]}' | mongo && echo 'rs.initiate(config)' | 
mongo"

echo "Completed!"

2

This is a sentence and image on a newline
http://imgur.com/image.gif

3

This is a sentence http://imgur.com/image.gif

当显示来自用户的这三种类型的注释时,我想让PHP仅检测第一种情况和第一种情况-换行符上的图像URL(以常见图像扩展名结尾的URL)并将其包装显示时在This is a sentence and an image with IMG tags entered by user already <img src="This is a sentence http://imgur.com/image.gif" alt="" /> 标签中显示。

考虑到注释字符串,我遇到的问题是PHP如何准确地检测到这样的图像链接?

我觉得这可以用正则表达式来完成,但是我从来没有熟练过它吗?还是更复杂?

1 个答案:

答案 0 :(得分:1)

您可以使用此正则表达式查看每行的开头((?m)^),任意水平的空白(\h*)(空格或制表符),然后查看HTTP协议({ {1}})。协议之后,它将使用任何非空格字符(https?://)(因为URL不能包含空格),直到允许的扩展名(\S+?)。

jpe?g|gif|png|tiff|svg

替换为:

(?m)^\h*(https?://?\S+?\.(?:jpe?g|gif|png|tiff|svg))

https://regex101.com/r/OXvsBl/3/