文本中的正则表达式 PayPal 交易 ID
Regexp PayPal Transaction ID from text
我 运行 一个接受贝宝付款的网站。当用户对他们的购买有疑问时,他们会通过自编码票务系统将他们的交易 ID 发送给我。
我想将交易 ID 转换为 URL(转换为 link 以提取交易信息)以使一切对我来说更快。
我知道交易 ID 的长度为 17 个字符。我该怎么做?
更新:
当网友post让我们说吧。 "Hi there, my name is John Doe. I made a purchase and I'm having issues with it. Transaction ID: XXXXXX"。我需要能够将 XXXX 变成 link 来提取交易信息。我不知道如何在 PHP 和 preg_replace 中创建它,所以它只将交易 ID 格式化为 link.
我有查找交易信息所需的 link,但我只是不知道如何使用正则表达式查找交易 ID,然后用 link 包裹它。
如果您知道交易 ID,则必须简单地使用此 link:
https://www.paypal.com/__LANGUAGE__/cgi-bin/webscr?cmd=_view-a-trans&id=__TRANSACTION_ID__
其中
__LANGUAGE__
是语言(2个字母,小写,例如en,fr,it...)
__TRANSACTION_ID__
是17个字符的交易id
样本link
https://www.paypal.com/fr/cgi-bin/webscr?cmd=_view-a-trans&id=7FB54205R7031312B
编辑
假设您想从此文本中提取交易 ID:
Hi there, my name is John Doe. I made a purchase and I'm having issues with it. Transaction ID: 7FB54205R7031312B.
然后你可以使用这个正则表达式:(?<=\s)([A-Z\d]{17})
示例PHP代码
$string = "Hi there, my name is John Doe. I made a purchase and I'm having issues with it. Transaction ID: 7FB54205R7031312B.";
$pattern = '/(?<=\s)([A-Z\d]{17})/';
preg_match($pattern, $string, $matches);
$transaction_ID = $matches[0];
$language = 'en';
$url = 'https://www.paypal.com/'.$language.'/cgi-bin/webscr?cmd=_view-a-trans&id='.$transaction_ID;
echo $url;
备注
Code will also work with text like Hi I'm John Doe. transaction: 7FB54205R7031312B can you help me?
你可能会选择:
\b[\dA-Z]{17}\b
其中查找长度为 17 且两边都有边框的字符串。如果你知道,它只有数字,这可能也是如此 ~\b\d{17}\b~
但你没有详细说明规格,真的。
在 PHP
中,这将是:
<?php
$string = <<<DATA
When users post let us say. "Hi there, my name is John Doe. I made a purchase and I'm having issues with it. Transaction ID: XXXXXXXXXXXXXXXXX". I need to be able to turn the XXXX into a link that pulls up transaction info. I can't figure out how to create this in PHP with preg_replace, so it only formats the transaction ID into the link.
I have the link that is needed to lookup the transaction info, but I just do not know how to use regex to find the transaction ID and then wrap a link around it.
DATA;
$regex = '~\b[\dA-Z]{17}\b~';
$string_with_links = preg_replace($regex, "http://www.exaple.com/id/[=11=]", $string);
echo $string_with_links;
?>
我 运行 一个接受贝宝付款的网站。当用户对他们的购买有疑问时,他们会通过自编码票务系统将他们的交易 ID 发送给我。
我想将交易 ID 转换为 URL(转换为 link 以提取交易信息)以使一切对我来说更快。
我知道交易 ID 的长度为 17 个字符。我该怎么做?
更新:
当网友post让我们说吧。 "Hi there, my name is John Doe. I made a purchase and I'm having issues with it. Transaction ID: XXXXXX"。我需要能够将 XXXX 变成 link 来提取交易信息。我不知道如何在 PHP 和 preg_replace 中创建它,所以它只将交易 ID 格式化为 link.
我有查找交易信息所需的 link,但我只是不知道如何使用正则表达式查找交易 ID,然后用 link 包裹它。
如果您知道交易 ID,则必须简单地使用此 link:
https://www.paypal.com/__LANGUAGE__/cgi-bin/webscr?cmd=_view-a-trans&id=__TRANSACTION_ID__
其中
__LANGUAGE__
是语言(2个字母,小写,例如en,fr,it...)
__TRANSACTION_ID__
是17个字符的交易id
样本link
https://www.paypal.com/fr/cgi-bin/webscr?cmd=_view-a-trans&id=7FB54205R7031312B
编辑
假设您想从此文本中提取交易 ID:
Hi there, my name is John Doe. I made a purchase and I'm having issues with it. Transaction ID: 7FB54205R7031312B.
然后你可以使用这个正则表达式:(?<=\s)([A-Z\d]{17})
示例PHP代码
$string = "Hi there, my name is John Doe. I made a purchase and I'm having issues with it. Transaction ID: 7FB54205R7031312B.";
$pattern = '/(?<=\s)([A-Z\d]{17})/';
preg_match($pattern, $string, $matches);
$transaction_ID = $matches[0];
$language = 'en';
$url = 'https://www.paypal.com/'.$language.'/cgi-bin/webscr?cmd=_view-a-trans&id='.$transaction_ID;
echo $url;
备注
Code will also work with text like Hi I'm John Doe. transaction: 7FB54205R7031312B can you help me?
你可能会选择:
\b[\dA-Z]{17}\b
其中查找长度为 17 且两边都有边框的字符串。如果你知道,它只有数字,这可能也是如此 ~\b\d{17}\b~
但你没有详细说明规格,真的。
在
PHP
中,这将是:
<?php
$string = <<<DATA
When users post let us say. "Hi there, my name is John Doe. I made a purchase and I'm having issues with it. Transaction ID: XXXXXXXXXXXXXXXXX". I need to be able to turn the XXXX into a link that pulls up transaction info. I can't figure out how to create this in PHP with preg_replace, so it only formats the transaction ID into the link.
I have the link that is needed to lookup the transaction info, but I just do not know how to use regex to find the transaction ID and then wrap a link around it.
DATA;
$regex = '~\b[\dA-Z]{17}\b~';
$string_with_links = preg_replace($regex, "http://www.exaple.com/id/[=11=]", $string);
echo $string_with_links;
?>