Reputation: 2817
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. 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?/
Upvotes: 2
Views: 518
Reputation: 789
I know this is answered but I think on also providing a regex
solution for this:
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. 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
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.
Upvotes: 0
Reputation: 38502
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() -
<?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
Upvotes: 1
Reputation: 5191
If the strings will always have the same number of values perhaps explode
and then using specific array positions would work for you.
$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];
Upvotes: 3