简体   繁体   English

正则表达式:匹配以http / https或仅site.com/开头的链接,但不带前缀吗?

[英]Regex: match a link that begins with http/https or just site.com/… but NOT with prefix?

I need to match links, but only those who don't begin with the string [image:x] where x is the link itself. 我需要匹配链接,但仅匹配那些不以字符串[image:x]开头的链接,其中x是链接本身。

I need this in a preg_replace so that only links that fit will be changed, here is an example: 我在preg_replace中需要它,以便仅更改适合的链接,这是一个示例:

"a dog once ate my book http://mysite.com/dog.jpg and I had to go to the store https://www.mysite.com/images/store.png and buy a new iPad apple.com/iPad/iPadMini.jpg And there it was fun [ image:http://apple.com/iPad/images/iPads.com ]" “一条狗曾经吃过我的书http://mysite.com/dog.jpg ,我不得不去商店https://www.mysite.com/images/store.png并购买一个新的iPad apple.com/iPad/iPadMini.jpg有趣的是[ image:http://apple.com/iPad/images/iPads.com ]”

would result as 将导致

"a dog once ate my book [image:http://mysite.com/dog.jpg] and I had to go to the store [image:https://www.mysite.com/images/store.png] and buy a new iPad [image:apple.com/iPad/iPadMini.jpg] And there it was fun [ image:http://apple.com/iPad/images/iPads.com ]" “一条狗曾经吃过我的书[image:http://mysite.com/dog.jpg] ,我不得不去商店[image:https://www.mysite.com/images/store.png] ,购买新的iPad [image:apple.com/iPad/iPadMini.jpg] ,这很有趣[ image:http://apple.com/iPad/images/iPads.com ]”

Notice the last link, as for now I get it doubled (the link is still picked up by preg_replace). 注意最后一个链接,因为现在我把它加倍了(该链接仍被preg_replace拾取)。 like here (not the desired effect, I need it untouched): 就像这里(不是想要的效果,我需要保持不变):

...[image:apple.com/iPad/iPadMini.jpg] And there it was fun ...[image:apple.com/iPad/iPadMini.jpg]那里很有趣
[ image:[image:http://apple.com/iPad/images/iPads.com]] " [ image:[image:http://apple.com/iPad/images/iPads.com]]

Here's my preg_replace as for now: 到目前为止,这是我的preg_replace:

preg_replace('~(https?://)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\S*)\.(jpg|png|jpeg|bmp|gif)~','[[image:$0]]',$text);

You can use a negative lookbehind . 您可以在后面使用负数

We don't want it to start with '[image:'. 我们不希望它以“ [image:””开头。 We also don't want to start in the middle of a word. 我们也不想从一个字开始。 this is prevented using \\b . 使用\\b可以防止这种情况。 If the string starts with http:// it should be included in the match, so don't match after http:// or https:// . 如果字符串以http://开头,则应将其包含在匹配项中,因此请不要在http://https://之后匹配。

preg_replace('~(?<!\[image:)(?<!http://)(?<!https://)(?<!\w\.)(https?://)?\b[\w-]+(\.[\w-]+)+\.?(:\d+)?(\S*)\.(jpg|png|jpeg|bmp|gif)~','[image:$0]',$text);

Check this PHP fiddle 检查这个PHP小提琴

Note: that also won't replace a string with the final ] missing, like [image:example.com . 注意:也不会替换最后一个字符串]失踪,像[image:example.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM