简体   繁体   English

php 中的正则表达式快速帮助

[英]Quick help with regex in php

Im not proficient in regex at all, but I need to strip IDs from urls, that are from a large block of text.我根本不精通正则表达式,但我需要从 url 中删除 ID,这些 url 来自大量文本。

URL look like this: URL 看起来像这样:

domain.com/path/ID_GOES_HERE domain.com/path/ID_GOES_HERE

The problem is, its inside emails, which come in a wide variety of formats ranging from:问题是,它的内部电子邮件有多种格式,包括:

- <a href="http://www.domain.com/path/ID_GOES_HERE">http://www.domain.com/path/ID_GOES_HERE</a>
- www.domain.com/path/ID_GOES_HERE
- http://domain.com/path/ID_GOES
_HERE

The ID is letters and numbers only. ID 只是字母和数字。 No other characters of any kind.没有其他任何类型的字符。

EDIT: Another issue is, since Im processing emails, which are horribly formatted, sometimes the URL ends up at the end of the line, where it gets broken up between 2 lines, which puts an equal sign at the end, like so:编辑:另一个问题是,由于我正在处理格式非常糟糕的电子邮件,因此有时 URL 会出现在行尾,它在两行之间被拆分,这会在末尾放置一个等号,如下所示:

http://www.domain.com/path/EE33FDE291A=
8D972

So the ID gets deformed.所以ID会变形。

This should do what you need:这应该做你需要的:

<?php
$matches = array();
preg_match_all('@domain\.com/path/((?:[a-z0-9_]|=\n)*)@i', $subject, $matches);
foreach ($matches[1] as $id) {
    $id = str_replace("=\n", '', $id);
    // Do your processing here.
}
preg_match('/^domain\.com\/path\/([a-zA-Z0-9]*)$/', $text, $matches = array());
if(isset($matches[1]))
  echo $matches[1];

try this regex试试这个正则表达式

/(?:https?:\/\/)?(?:www.)?domain.com/path/([\d\w]+(?:\=?(?:\(?:[\r\n]|\r\n|)(?:[\d\w]+)?)?)/

seems to match all of your test cases似乎匹配你所有的测试用例

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

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