简体   繁体   English

从字符串中提取数据

[英]Extracting data from a string

I have a string and want to extract data from it. 我有一个字符串,想从中提取数据。

$str = "Online (UVD) - 154,842 - Last Updated: Nov 23 2015 02:24 PM";

I want this 154,842 extract and this 2015 I've successfully extracted the first part. 我想要这154,842摘录,而这个2015我已经成功摘录了第一部分。 with this method 用这种方法

trim(str_replace("Online (UVD) - ", "", str_replace(",", "", substr_replace($str, "", strpos($str, " - Last Updated"))), $str))

Now, I'm unsure how to extract the other one. 现在,我不确定如何提取另一个。 Data can vary for instance, 数据可能会有所不同,例如

$str = "Online (UVD) - 1123123 - Last Updated: Nov 23 2015 02:24 PM";
$str = "Online (UVD) - 12 - Last Updated: Nov 23 2015 02:24 PM";
$str = "Online (UVD) - 1546546 - Last Updated: Nov 23 2015 02:24 PM";
$str = "Online (UVD) - 3525252525 - Last Updated: Nov 23 2015 02:24 PM";

Is there a better method to extract?/ 有更好的提取方法吗?/

If the strings will always have the same number of values perhaps explode and then using specific array positions would work for you. 如果字符串将始终具有相同数量的值,则可能explode ,然后使用特定的数组位置将对您有用。

$str = "Online (UVD) - 154,842 - Last Updated: Nov 23 2015 02:24 PM";
$pieces = explode(' ',$str);
echo 'Value is ' . $pieces[3] . ' and the year is ' . $pieces[9];

You can do it without using regex if all the words in the string are in same order that you provided. 如果字符串中的所有单词都与您提供的顺序相同, 无需使用正则表达式就可以做到这一点。 Let's try with explode() - 让我们尝试explode() -

<?php
$str = "Online (UVD) - 1123123 - Last Updated: Nov 23 2015 02:24 PM";
$str = "Online (UVD) - 12 - Last Updated: Nov 23 2015 02:24 PM";
$str = "Online (UVD) - 1546546 - Last Updated: Nov 23 2015 02:24 PM";
$str = "Online (UVD) - 3525252525 - Last Updated: Nov 23 2015 02:24 PM";

$digit = explode(' ',$str);
echo trim($digit[3]); // returns digits
echo trim($digit[9]); // returns date
?>

DEMO: https://3v4l.org/ttBDG 演示: https : //3v4l.org/ttBDG

I know this is answered but I think on also providing a regex solution for this: 我知道已经回答了,但是我想也为此提供regex解决方案:

To extract your 1st group , you can use bellow regex: 要提取第一组 ,可以使用以下正则表达式:

preg_match('/.-.(\d+).-/', $str, $numExtracted);

if (!empty($numExtracted)) {
    echo $numExtracted[1].PHP_EOL;   
}

To extract your Year : 提取年份

preg_match('/(\w\w\w).(\d\d).(\d\d\d\d)/', $str, $year, PREG_OFFSET_CAPTURE);
$year = $year[3][0];
echo $year.PHP_EOL;

This worked on all of the below trials: 这适用于以下所有试验:

Online (UVD) - 1123123 - Last Updated: Nov 23 2015 02:24 PM
Online (UVD) - 12 - Last Updated: Nov 23 2015 02:24 PM
Online (UVD) oi oi    -            1546546 - Last Updated: Nov 23 2015 02:24 PM
Online -sdtgstg346fg - (UVD) - 3525252525 - Last Updated:             Nov 23 2015 02:24 PM

You can check the working code here 您可以在这里查看工作代码

As per you comment question, you can enhance your regex to consider such cases: 根据您的评论问题,您可以增强正则表达式以考虑以下情况:

.-.(\d+)?[\,\#\!\?\$\£\;\:]*(\d+)?.-

It will match all of the above plus this cases: 它将匹配以上所有条件以及以下情况:

Online (UVD) - 1123,123 - Last Updated: Nov 23 2015 02:24 PM
Online (UVD) - 1123#!,123 - Last Updated: Nov 23 2015 02:24 PM

But I think there is a time you need to consider if you want to have a hold on the information you received or just consider it corrupt. 但是我认为您需要考虑一下是否要保留收到的信息,或者只是认为信息已损坏。

You can even introduce cycles to parse to every single case scenario but if I am expecting a number and suddenly the regex that triggers a match is for something like 1A2B3C4G5D8D2F I will discard it as it goes far from what I initially expected. 您甚至可以引入周期来解析每种情况,但是如果我期望有一个数字,而突然触发匹配的regex适用于1A2B3C4G5D8D2F类的1A2B3C4G5D8D2F我会丢弃它,因为它与最初的预期相去甚远。 But it all depends from where you receive your information, how likely is it to change, etc :) 但这一切都取决于您从何处接收信息,信息被更改的可能性如何,等等:)

Still, I think regex will make you happier and assert far more possibilities 不过,我认为regex会让您更快乐,并断言更多的可能性

PS: For the special cases introduced, because the number is interrupted by special chars (or even words if you consider them) it now interprets and 2 numbers. PS:对于所介绍的特殊情况,由于数字被特殊字符(甚至是单词,如果您认为它们也被打断了),现在可以解释和2个数字。

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

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