将多行联系人卡片解析为单独的变量

发布于 2024-09-25 07:43:30 字数 453 浏览 3 评论 0原文

我想获取以这种格式输入的数据:

John Smith
123 Fake Street
Fake City, 55555
http://website.com

并将值存储在变量中,如下所示:

$name = 'John Smith';
$address = '123 Fake Street';
$city = 'Fake City';
$zip = '55555';
$website = 'http://website.com';
  • 名称将是第一行上输入的任何内容
  • 地址是第二行上输入的任何内容
  • 城市是第三行逗号分隔符之前输入的任何内容
  • zip 是第三行逗号后面的内容 网站
  • 是第五行的内容。

我不希望模式规则比这更严格。有人可以展示如何做到这一点吗?

I want to take data entered in this format:

John Smith
123 Fake Street
Fake City, 55555
http://website.com

and store the values in variables like so:

$name = 'John Smith';
$address = '123 Fake Street';
$city = 'Fake City';
$zip = '55555';
$website = 'http://website.com';
  • name will be whatever is entered on the first line
  • address is whatever is on the second line
  • city is whatever is entered on the third line before the comma separator
  • zip is whatever is on the third line after the comma
    and
  • website is whatever is on the fifth line.

I don't want the pattern rules to be stricter than that. Can someone please show how this can be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苦笑流年记忆 2024-10-02 07:43:30
$data = explode("\n", $input);

$name    = $data[0];
$address = $data[1];
$website = $data[3];

$place   = explode(',', $data[2]);

$city    = $place[0];
$zip     = $place[1];
$data = explode("\n", $input);

$name    = $data[0];
$address = $data[1];
$website = $data[3];

$place   = explode(',', $data[2]);

$city    = $place[0];
$zip     = $place[1];
離殇 2024-10-02 07:43:30

好吧,正则表达式可能类似于:

([^\r]+)\r([^\r]+)\r([^,]+),\s+?([^\r]+)\r(.+)

假设 \r 是换行符分隔符。当然,使用 explode() 之类的东西来分割会更容易事物在线条中...

Well, the regex would probably be something like:

([^\r]+)\r([^\r]+)\r([^,]+),\s+?([^\r]+)\r(.+)

Assuming that \r is your newline separator. Of course, it'd be even easier to use something like explode() to split things in to lines...

横笛休吹塞上声 2024-10-02 07:43:30

如果你想要更精确的东西,你可以使用这个:

$matches = array();

if (preg_match('/(?P<firstName>.*?)\\r(?P<streetAddress>.*?)\\r(?P<city>.*?)\\,\\s?(?P<zipCode>.*?)\\r(?P<website>.*)\\r/s', $subject, $matches)) {
   var_dump( $matches ); // will print an array with the parts
} else {
   throw new Exception( 'unable to parse data' );
}

干杯

If you want something more precise, you could use this:

$matches = array();

if (preg_match('/(?P<firstName>.*?)\\r(?P<streetAddress>.*?)\\r(?P<city>.*?)\\,\\s?(?P<zipCode>.*?)\\r(?P<website>.*)\\r/s', $subject, $matches)) {
   var_dump( $matches ); // will print an array with the parts
} else {
   throw new Exception( 'unable to parse data' );
}

cheers

梦晓ヶ微光ヅ倾城 2024-10-02 07:43:30

sscanf() 旨在解析可预测格式的文本。它可以返回捕获子字符串的数组,填充引用变量(如果它们被声明为参数)。

只需知道每个占位符都会贪婪地匹配,%s不会跨越空格,一旦不满足占位符,捕获就会停止,并且后续变量将不会被分配。

代码:(演示

$card = <<<CARD
John Smith
123 Fake Street
Fake City, 55555
http://website.com
CARD;

sscanf(
    $card,
    "%[^\n]
     %[^\n]
     %[^,],%s
     %s",
    $name,
    $address,
    $city,
    $zip,
    $website
);
var_dump($name, $address, $city, $zip, $website);

输出:

string(10) "John Smith"
string(15) "123 Fake Street"
string(9) "Fake City"
string(5) "55555"
string(18) "http://website.com"

sscanf() is designed to parse predictably formatted text. It can return an array of captured substrings, populate reference variables if they are declared as parameters.

Just know that each placeholder will greedily match, %s will not cross a whitespace, and capturing will halt as soon as a placeholder is not satisfied and subsequent variables will not be assigned.

Code: (Demo)

$card = <<<CARD
John Smith
123 Fake Street
Fake City, 55555
http://website.com
CARD;

sscanf(
    $card,
    "%[^\n]
     %[^\n]
     %[^,],%s
     %s",
    $name,
    $address,
    $city,
    $zip,
    $website
);
var_dump($name, $address, $city, $zip, $website);

Output:

string(10) "John Smith"
string(15) "123 Fake Street"
string(9) "Fake City"
string(5) "55555"
string(18) "http://website.com"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文