简体   繁体   English

php正则表达式解析mailTo

[英]php regex to parse mailTo

I have a following html source string: 我有一个以下的html源代码字符串:

<a href="mailto:abcd@test.com?body=This%20is%20the%20body%20-123-&subject=Subject%20Text&Content-Type=text/plain">Reply To Post</a>

From the above string I want to extract: 从上面的字符串我想提取:

  1. Email address that is the part after mailto: and before ? mailto:之前的电子邮件地址以及之前的部分?
  2. Body 身体
  3. Subject 学科

Any help with the regex will be appreciated. 任何有关正则表达式的帮助将不胜感激。 Thanks in advance. 提前致谢。

You would need not need regex for the second part. 第二部分你不需要正则表达式。 It can be parsed as a query string , IMO. 它可以解析为查询字符串 IMO。

Something like: ( $s is the value of href in the following code) 类似于:( $s是以下代码中的href值)

preg_match("/mailto:(.*?)\?(.*)/",$s,$matches);

echo "Email:" . $matches[1] . "\n";
parse_str($matches[2],$output);
echo "Body: " . $output['body'] . "\n";
echo "Subject: " . $output['subject'] . "\n";

Actually, if you are sure the string appears in the exact same fashion, you could take the substring from the offset of index of ":" up to the index of "?", too. 实际上,如果您确定字符串以完全相同的方式出现,您可以将索引偏移量为“:”的子字符串转换为索引“?”。

This will assume you only a single mailto link: 这将假设您只有一个mailto链接:

// $str will be your string content from the question
if (preg_match('/"mailto:([^"]+?)/', $str, $matches) && false !== ($info = parse_url($matches[1]))) {
        $emailAddress = $info['path'];
        $emailParameters = array();
        if (isset($info['query'])) {
                parse_str($info['query'], $emailParameters);
        }
        var_dump($emailAddress, $emailParameters);
}

It matches from the "mailto: to the first end quote and uses parse_url to do the rest. 它匹配"mailto:到第一个结束引用,并使用parse_url完成剩下的工作。

Haven't tried it in PHP, but it works fine in Regex Hero : 没有在PHP中尝试过,但它在Regex Hero中工作正常:

"mailto:([\\w%.+-]+?@[\\w.-]+?)(?:[?&](?:body=(.*?)|subject=(.*?)|[\\w-]+=.*?))+?"

This should result in the following capture groups: 这应该会产生以下捕获组:

  • 1: email address 1:电子邮件地址
  • 2: body 2:身体
  • 3: subject 3:主题

You might want to do some more intensive testing though, as I'm not sure whether I've got all valid mail addresses. 您可能想要进行更密集的测试,因为我不确定我是否拥有所有有效的邮件地址。

Try this 尝试这个

$m = preg_match("/mailto:(.+?)\?/");

it matches the word mailto followed by a colon, followed by a capturing group (parenthesis) which contains any character . 它匹配单词mailto后跟冒号,后跟一个包含任何字符的捕获组(括号) . one or more times + un-greedily (? - it will make the capture as short as possible) followed by a (escaped) question mark ( \\? ) 一次或多次+非贪婪(? - 它会使捕获尽可能短)然后是(转义)问号( \\?

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

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