PHP - Regex to get integer value that formatted separated by character
If I have a string like this :
TAX ID NO. 01.000.589.0.055.000
My goal is, I want to get the integer digit after tax
phrase, which is 010005890055000
So far I write like this
/(tax id no)s*[^A-Za-z0-9]?s*Kd+/i
but It still not true because sometimes user wrote like this:
TAX ID. 01.000.589.0.055.000 or
TAX ID : 01.000.589.0.055.000 or
TAX ID 01.000.589.0.055.000 or
TAX: 01.000.589.0.055.000
even user wrote directly into a prefect integer digit
TAX ID NO. 010005890055000
Please advise
This is all my possibilities:
Non Formatted
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000
Formatted
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.000.589.0.055.000
With different
tax
phrase
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID. 010005890055000
In beginning a sentence
TAX ID. 010005890055000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
TAX ID. 01.000.589.0.055.000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
So much thank you...
php regex
add a comment |
If I have a string like this :
TAX ID NO. 01.000.589.0.055.000
My goal is, I want to get the integer digit after tax
phrase, which is 010005890055000
So far I write like this
/(tax id no)s*[^A-Za-z0-9]?s*Kd+/i
but It still not true because sometimes user wrote like this:
TAX ID. 01.000.589.0.055.000 or
TAX ID : 01.000.589.0.055.000 or
TAX ID 01.000.589.0.055.000 or
TAX: 01.000.589.0.055.000
even user wrote directly into a prefect integer digit
TAX ID NO. 010005890055000
Please advise
This is all my possibilities:
Non Formatted
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000
Formatted
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.000.589.0.055.000
With different
tax
phrase
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID. 010005890055000
In beginning a sentence
TAX ID. 010005890055000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
TAX ID. 01.000.589.0.055.000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
So much thank you...
php regex
add a comment |
If I have a string like this :
TAX ID NO. 01.000.589.0.055.000
My goal is, I want to get the integer digit after tax
phrase, which is 010005890055000
So far I write like this
/(tax id no)s*[^A-Za-z0-9]?s*Kd+/i
but It still not true because sometimes user wrote like this:
TAX ID. 01.000.589.0.055.000 or
TAX ID : 01.000.589.0.055.000 or
TAX ID 01.000.589.0.055.000 or
TAX: 01.000.589.0.055.000
even user wrote directly into a prefect integer digit
TAX ID NO. 010005890055000
Please advise
This is all my possibilities:
Non Formatted
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000
Formatted
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.000.589.0.055.000
With different
tax
phrase
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID. 010005890055000
In beginning a sentence
TAX ID. 010005890055000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
TAX ID. 01.000.589.0.055.000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
So much thank you...
php regex
If I have a string like this :
TAX ID NO. 01.000.589.0.055.000
My goal is, I want to get the integer digit after tax
phrase, which is 010005890055000
So far I write like this
/(tax id no)s*[^A-Za-z0-9]?s*Kd+/i
but It still not true because sometimes user wrote like this:
TAX ID. 01.000.589.0.055.000 or
TAX ID : 01.000.589.0.055.000 or
TAX ID 01.000.589.0.055.000 or
TAX: 01.000.589.0.055.000
even user wrote directly into a prefect integer digit
TAX ID NO. 010005890055000
Please advise
This is all my possibilities:
Non Formatted
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000
Formatted
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.000.589.0.055.000
With different
tax
phrase
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID. 010005890055000
In beginning a sentence
TAX ID. 010005890055000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
TAX ID. 01.000.589.0.055.000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
So much thank you...
php regex
php regex
edited Nov 20 '18 at 7:04
Fadly Dzil
asked Nov 20 '18 at 6:21
Fadly DzilFadly Dzil
8251540
8251540
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
It looks like your only consistent keyword is TAX
, so you can grab digits (with dots and dashes) after it with the regex ~TAX.*?.? *?([d.-]+)~i
, then strip non-digits (if desired). Here's an example test:
$data = "JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID. 010005890055000
TAX ID. 010005890055000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
tax ID. 01.000.589.0.055.000 , JL
TAX ID. 01.000.589.0.055.000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
JL. CAKUNG CILINCING KP BARU RT 007/008 CAKUNG BARAT, CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.109.421.6-902.000";
foreach (explode("n", $data) as $line) {
preg_match("~TAX.*?.? *?([d.-]+)~i", $line, $m);
echo preg_replace("~D~", "", $m[1]) . "n";
}
Output:
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
011094216902000
Try it!
Sorry, it is not in a line.
– Fadly Dzil
Nov 20 '18 at 6:35
OK, this is just an example.preg_replace("~D~", "", $line)
works on all of your test cases regardless of whether they're individual strings or not. If not, let me know what sort of input you are dealing with that isn't working and I'll update.
– ggorlen
Nov 20 '18 at 6:37
Thanks for the effort, but how with formatted number as explained above ?
– Fadly Dzil
Nov 20 '18 at 6:46
1
Can you list all of your possible input cases in the question from the beginning? It's not helpful for you or us to continually adjust the requirements.
– ggorlen
Nov 20 '18 at 6:50
1
Working as charm, thanks. Sorry if I asked not clearly.Basically from my bad english.
– Fadly Dzil
Nov 20 '18 at 7:23
|
show 8 more comments
You can make use of this:
/(?:taxs*(?:ids*)?).*?(d[d.]+)/i
The first group checks for tax
with id
being optional. It then grabs the first digit and all digit and .
and groups it under $1
Regexr
add a comment |
You can use preg_replace to remove all but numbers.
preg_replace('/[^d]/', '', $str);
see output: https://3v4l.org/Ne3fo
To answer the new requirement:
Use substr and strpos to remove the part up to "tax".
$arr = ["TAX ID. 01.000.589.0.055.000 or",
"TAX ID : 01.000.589.0.055.000 or",
"TAX ID 01.000.589.0.055.000 or",
"TAX: 01.000.589.0.055.000",
"ARU RT 007/008 CAKUNG BARAT, CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000"];
foreach($arr as $str){
echo preg_replace('/[^d]/', '', substr($str, stripos($str, "tax"))) . "n";
}
https://3v4l.org/btDAq
Sorry, basically its part of long string, ex "JAKARTA TIMUR 13910 INDONESIA TAX ID 01.328.570.5-007.000"
– Fadly Dzil
Nov 20 '18 at 6:36
1
in that case the output becomes 13910013285705007000. Keep in mind what your question says.
– Andreas
Nov 20 '18 at 6:38
1
Clearly after 'tax' phrase. Thanks
– Fadly Dzil
Nov 20 '18 at 6:41
1
If I have a string like this : and but It still not true because sometimes user wrote like this:. Nothing is clear unless you make it clear!
– Andreas
Nov 20 '18 at 6:42
Okey, thanks. Sorry.
– Fadly Dzil
Nov 20 '18 at 6:46
add a comment |
You can use this regex, where except TAX part, I have made everything that comes in middle between tax and numbers as optional. Use this regex and capture group1
/tax(?:.*?)s+([d.]+)/i
Demo
Once you get digit and dot part, remove all dot characters from it.
Here is a sample php code,
$arr = ["TAX ID NO. 01.000.589.0.055.000","TAX ID. 01.000.589.0.055.000","TAX ID : 01.000.589.0.055.000","TAX ID 01.000.589.0.055.000","TAX: 01.000.589.0.055.000", "TAX ID NO. 010005890055000"];
foreach ($arr as &$value) {
preg_match('/tax(?:.*?)s+([d.]+)/i', $value, $matches);
echo $value . " --> " . preg_replace('/./','',$matches[1]) . "n";
}
This prints following output,
TAX ID NO. 01.000.589.0.055.000 --> 010005890055000
TAX ID. 01.000.589.0.055.000 --> 010005890055000
TAX ID : 01.000.589.0.055.000 --> 010005890055000
TAX ID 01.000.589.0.055.000 --> 010005890055000
TAX: 01.000.589.0.055.000 --> 010005890055000
TAX ID NO. 010005890055000 --> 010005890055000
1
I believe this one could work even with the new string
– Andreas
Nov 20 '18 at 6:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387322%2fphp-regex-to-get-integer-value-that-formatted-separated-by-character%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like your only consistent keyword is TAX
, so you can grab digits (with dots and dashes) after it with the regex ~TAX.*?.? *?([d.-]+)~i
, then strip non-digits (if desired). Here's an example test:
$data = "JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID. 010005890055000
TAX ID. 010005890055000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
tax ID. 01.000.589.0.055.000 , JL
TAX ID. 01.000.589.0.055.000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
JL. CAKUNG CILINCING KP BARU RT 007/008 CAKUNG BARAT, CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.109.421.6-902.000";
foreach (explode("n", $data) as $line) {
preg_match("~TAX.*?.? *?([d.-]+)~i", $line, $m);
echo preg_replace("~D~", "", $m[1]) . "n";
}
Output:
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
011094216902000
Try it!
Sorry, it is not in a line.
– Fadly Dzil
Nov 20 '18 at 6:35
OK, this is just an example.preg_replace("~D~", "", $line)
works on all of your test cases regardless of whether they're individual strings or not. If not, let me know what sort of input you are dealing with that isn't working and I'll update.
– ggorlen
Nov 20 '18 at 6:37
Thanks for the effort, but how with formatted number as explained above ?
– Fadly Dzil
Nov 20 '18 at 6:46
1
Can you list all of your possible input cases in the question from the beginning? It's not helpful for you or us to continually adjust the requirements.
– ggorlen
Nov 20 '18 at 6:50
1
Working as charm, thanks. Sorry if I asked not clearly.Basically from my bad english.
– Fadly Dzil
Nov 20 '18 at 7:23
|
show 8 more comments
It looks like your only consistent keyword is TAX
, so you can grab digits (with dots and dashes) after it with the regex ~TAX.*?.? *?([d.-]+)~i
, then strip non-digits (if desired). Here's an example test:
$data = "JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID. 010005890055000
TAX ID. 010005890055000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
tax ID. 01.000.589.0.055.000 , JL
TAX ID. 01.000.589.0.055.000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
JL. CAKUNG CILINCING KP BARU RT 007/008 CAKUNG BARAT, CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.109.421.6-902.000";
foreach (explode("n", $data) as $line) {
preg_match("~TAX.*?.? *?([d.-]+)~i", $line, $m);
echo preg_replace("~D~", "", $m[1]) . "n";
}
Output:
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
011094216902000
Try it!
Sorry, it is not in a line.
– Fadly Dzil
Nov 20 '18 at 6:35
OK, this is just an example.preg_replace("~D~", "", $line)
works on all of your test cases regardless of whether they're individual strings or not. If not, let me know what sort of input you are dealing with that isn't working and I'll update.
– ggorlen
Nov 20 '18 at 6:37
Thanks for the effort, but how with formatted number as explained above ?
– Fadly Dzil
Nov 20 '18 at 6:46
1
Can you list all of your possible input cases in the question from the beginning? It's not helpful for you or us to continually adjust the requirements.
– ggorlen
Nov 20 '18 at 6:50
1
Working as charm, thanks. Sorry if I asked not clearly.Basically from my bad english.
– Fadly Dzil
Nov 20 '18 at 7:23
|
show 8 more comments
It looks like your only consistent keyword is TAX
, so you can grab digits (with dots and dashes) after it with the regex ~TAX.*?.? *?([d.-]+)~i
, then strip non-digits (if desired). Here's an example test:
$data = "JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID. 010005890055000
TAX ID. 010005890055000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
tax ID. 01.000.589.0.055.000 , JL
TAX ID. 01.000.589.0.055.000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
JL. CAKUNG CILINCING KP BARU RT 007/008 CAKUNG BARAT, CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.109.421.6-902.000";
foreach (explode("n", $data) as $line) {
preg_match("~TAX.*?.? *?([d.-]+)~i", $line, $m);
echo preg_replace("~D~", "", $m[1]) . "n";
}
Output:
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
011094216902000
Try it!
It looks like your only consistent keyword is TAX
, so you can grab digits (with dots and dashes) after it with the regex ~TAX.*?.? *?([d.-]+)~i
, then strip non-digits (if desired). Here's an example test:
$data = "JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX 010005890055000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID 01.000.589.0.055.000
JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID. 010005890055000
TAX ID. 010005890055000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
tax ID. 01.000.589.0.055.000 , JL
TAX ID. 01.000.589.0.055.000 , JL. CACING RT 007/008 CAKUNG, JAKARTA TIMUR 13910 INDONESIA
JL. CAKUNG CILINCING KP BARU RT 007/008 CAKUNG BARAT, CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 01.109.421.6-902.000";
foreach (explode("n", $data) as $line) {
preg_match("~TAX.*?.? *?([d.-]+)~i", $line, $m);
echo preg_replace("~D~", "", $m[1]) . "n";
}
Output:
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
010005890055000
011094216902000
Try it!
edited Nov 20 '18 at 7:17
answered Nov 20 '18 at 6:33
ggorlenggorlen
7,1883825
7,1883825
Sorry, it is not in a line.
– Fadly Dzil
Nov 20 '18 at 6:35
OK, this is just an example.preg_replace("~D~", "", $line)
works on all of your test cases regardless of whether they're individual strings or not. If not, let me know what sort of input you are dealing with that isn't working and I'll update.
– ggorlen
Nov 20 '18 at 6:37
Thanks for the effort, but how with formatted number as explained above ?
– Fadly Dzil
Nov 20 '18 at 6:46
1
Can you list all of your possible input cases in the question from the beginning? It's not helpful for you or us to continually adjust the requirements.
– ggorlen
Nov 20 '18 at 6:50
1
Working as charm, thanks. Sorry if I asked not clearly.Basically from my bad english.
– Fadly Dzil
Nov 20 '18 at 7:23
|
show 8 more comments
Sorry, it is not in a line.
– Fadly Dzil
Nov 20 '18 at 6:35
OK, this is just an example.preg_replace("~D~", "", $line)
works on all of your test cases regardless of whether they're individual strings or not. If not, let me know what sort of input you are dealing with that isn't working and I'll update.
– ggorlen
Nov 20 '18 at 6:37
Thanks for the effort, but how with formatted number as explained above ?
– Fadly Dzil
Nov 20 '18 at 6:46
1
Can you list all of your possible input cases in the question from the beginning? It's not helpful for you or us to continually adjust the requirements.
– ggorlen
Nov 20 '18 at 6:50
1
Working as charm, thanks. Sorry if I asked not clearly.Basically from my bad english.
– Fadly Dzil
Nov 20 '18 at 7:23
Sorry, it is not in a line.
– Fadly Dzil
Nov 20 '18 at 6:35
Sorry, it is not in a line.
– Fadly Dzil
Nov 20 '18 at 6:35
OK, this is just an example.
preg_replace("~D~", "", $line)
works on all of your test cases regardless of whether they're individual strings or not. If not, let me know what sort of input you are dealing with that isn't working and I'll update.– ggorlen
Nov 20 '18 at 6:37
OK, this is just an example.
preg_replace("~D~", "", $line)
works on all of your test cases regardless of whether they're individual strings or not. If not, let me know what sort of input you are dealing with that isn't working and I'll update.– ggorlen
Nov 20 '18 at 6:37
Thanks for the effort, but how with formatted number as explained above ?
– Fadly Dzil
Nov 20 '18 at 6:46
Thanks for the effort, but how with formatted number as explained above ?
– Fadly Dzil
Nov 20 '18 at 6:46
1
1
Can you list all of your possible input cases in the question from the beginning? It's not helpful for you or us to continually adjust the requirements.
– ggorlen
Nov 20 '18 at 6:50
Can you list all of your possible input cases in the question from the beginning? It's not helpful for you or us to continually adjust the requirements.
– ggorlen
Nov 20 '18 at 6:50
1
1
Working as charm, thanks. Sorry if I asked not clearly.Basically from my bad english.
– Fadly Dzil
Nov 20 '18 at 7:23
Working as charm, thanks. Sorry if I asked not clearly.Basically from my bad english.
– Fadly Dzil
Nov 20 '18 at 7:23
|
show 8 more comments
You can make use of this:
/(?:taxs*(?:ids*)?).*?(d[d.]+)/i
The first group checks for tax
with id
being optional. It then grabs the first digit and all digit and .
and groups it under $1
Regexr
add a comment |
You can make use of this:
/(?:taxs*(?:ids*)?).*?(d[d.]+)/i
The first group checks for tax
with id
being optional. It then grabs the first digit and all digit and .
and groups it under $1
Regexr
add a comment |
You can make use of this:
/(?:taxs*(?:ids*)?).*?(d[d.]+)/i
The first group checks for tax
with id
being optional. It then grabs the first digit and all digit and .
and groups it under $1
Regexr
You can make use of this:
/(?:taxs*(?:ids*)?).*?(d[d.]+)/i
The first group checks for tax
with id
being optional. It then grabs the first digit and all digit and .
and groups it under $1
Regexr
answered Nov 20 '18 at 6:30
Amit JokiAmit Joki
47.1k75180
47.1k75180
add a comment |
add a comment |
You can use preg_replace to remove all but numbers.
preg_replace('/[^d]/', '', $str);
see output: https://3v4l.org/Ne3fo
To answer the new requirement:
Use substr and strpos to remove the part up to "tax".
$arr = ["TAX ID. 01.000.589.0.055.000 or",
"TAX ID : 01.000.589.0.055.000 or",
"TAX ID 01.000.589.0.055.000 or",
"TAX: 01.000.589.0.055.000",
"ARU RT 007/008 CAKUNG BARAT, CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000"];
foreach($arr as $str){
echo preg_replace('/[^d]/', '', substr($str, stripos($str, "tax"))) . "n";
}
https://3v4l.org/btDAq
Sorry, basically its part of long string, ex "JAKARTA TIMUR 13910 INDONESIA TAX ID 01.328.570.5-007.000"
– Fadly Dzil
Nov 20 '18 at 6:36
1
in that case the output becomes 13910013285705007000. Keep in mind what your question says.
– Andreas
Nov 20 '18 at 6:38
1
Clearly after 'tax' phrase. Thanks
– Fadly Dzil
Nov 20 '18 at 6:41
1
If I have a string like this : and but It still not true because sometimes user wrote like this:. Nothing is clear unless you make it clear!
– Andreas
Nov 20 '18 at 6:42
Okey, thanks. Sorry.
– Fadly Dzil
Nov 20 '18 at 6:46
add a comment |
You can use preg_replace to remove all but numbers.
preg_replace('/[^d]/', '', $str);
see output: https://3v4l.org/Ne3fo
To answer the new requirement:
Use substr and strpos to remove the part up to "tax".
$arr = ["TAX ID. 01.000.589.0.055.000 or",
"TAX ID : 01.000.589.0.055.000 or",
"TAX ID 01.000.589.0.055.000 or",
"TAX: 01.000.589.0.055.000",
"ARU RT 007/008 CAKUNG BARAT, CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000"];
foreach($arr as $str){
echo preg_replace('/[^d]/', '', substr($str, stripos($str, "tax"))) . "n";
}
https://3v4l.org/btDAq
Sorry, basically its part of long string, ex "JAKARTA TIMUR 13910 INDONESIA TAX ID 01.328.570.5-007.000"
– Fadly Dzil
Nov 20 '18 at 6:36
1
in that case the output becomes 13910013285705007000. Keep in mind what your question says.
– Andreas
Nov 20 '18 at 6:38
1
Clearly after 'tax' phrase. Thanks
– Fadly Dzil
Nov 20 '18 at 6:41
1
If I have a string like this : and but It still not true because sometimes user wrote like this:. Nothing is clear unless you make it clear!
– Andreas
Nov 20 '18 at 6:42
Okey, thanks. Sorry.
– Fadly Dzil
Nov 20 '18 at 6:46
add a comment |
You can use preg_replace to remove all but numbers.
preg_replace('/[^d]/', '', $str);
see output: https://3v4l.org/Ne3fo
To answer the new requirement:
Use substr and strpos to remove the part up to "tax".
$arr = ["TAX ID. 01.000.589.0.055.000 or",
"TAX ID : 01.000.589.0.055.000 or",
"TAX ID 01.000.589.0.055.000 or",
"TAX: 01.000.589.0.055.000",
"ARU RT 007/008 CAKUNG BARAT, CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000"];
foreach($arr as $str){
echo preg_replace('/[^d]/', '', substr($str, stripos($str, "tax"))) . "n";
}
https://3v4l.org/btDAq
You can use preg_replace to remove all but numbers.
preg_replace('/[^d]/', '', $str);
see output: https://3v4l.org/Ne3fo
To answer the new requirement:
Use substr and strpos to remove the part up to "tax".
$arr = ["TAX ID. 01.000.589.0.055.000 or",
"TAX ID : 01.000.589.0.055.000 or",
"TAX ID 01.000.589.0.055.000 or",
"TAX: 01.000.589.0.055.000",
"ARU RT 007/008 CAKUNG BARAT, CAKUNG, JAKARTA TIMUR 13910 INDONESIA TAX ID NO. 010005890055000"];
foreach($arr as $str){
echo preg_replace('/[^d]/', '', substr($str, stripos($str, "tax"))) . "n";
}
https://3v4l.org/btDAq
edited Nov 20 '18 at 6:46
answered Nov 20 '18 at 6:34
AndreasAndreas
16.1k31642
16.1k31642
Sorry, basically its part of long string, ex "JAKARTA TIMUR 13910 INDONESIA TAX ID 01.328.570.5-007.000"
– Fadly Dzil
Nov 20 '18 at 6:36
1
in that case the output becomes 13910013285705007000. Keep in mind what your question says.
– Andreas
Nov 20 '18 at 6:38
1
Clearly after 'tax' phrase. Thanks
– Fadly Dzil
Nov 20 '18 at 6:41
1
If I have a string like this : and but It still not true because sometimes user wrote like this:. Nothing is clear unless you make it clear!
– Andreas
Nov 20 '18 at 6:42
Okey, thanks. Sorry.
– Fadly Dzil
Nov 20 '18 at 6:46
add a comment |
Sorry, basically its part of long string, ex "JAKARTA TIMUR 13910 INDONESIA TAX ID 01.328.570.5-007.000"
– Fadly Dzil
Nov 20 '18 at 6:36
1
in that case the output becomes 13910013285705007000. Keep in mind what your question says.
– Andreas
Nov 20 '18 at 6:38
1
Clearly after 'tax' phrase. Thanks
– Fadly Dzil
Nov 20 '18 at 6:41
1
If I have a string like this : and but It still not true because sometimes user wrote like this:. Nothing is clear unless you make it clear!
– Andreas
Nov 20 '18 at 6:42
Okey, thanks. Sorry.
– Fadly Dzil
Nov 20 '18 at 6:46
Sorry, basically its part of long string, ex "JAKARTA TIMUR 13910 INDONESIA TAX ID 01.328.570.5-007.000"
– Fadly Dzil
Nov 20 '18 at 6:36
Sorry, basically its part of long string, ex "JAKARTA TIMUR 13910 INDONESIA TAX ID 01.328.570.5-007.000"
– Fadly Dzil
Nov 20 '18 at 6:36
1
1
in that case the output becomes 13910013285705007000. Keep in mind what your question says.
– Andreas
Nov 20 '18 at 6:38
in that case the output becomes 13910013285705007000. Keep in mind what your question says.
– Andreas
Nov 20 '18 at 6:38
1
1
Clearly after 'tax' phrase. Thanks
– Fadly Dzil
Nov 20 '18 at 6:41
Clearly after 'tax' phrase. Thanks
– Fadly Dzil
Nov 20 '18 at 6:41
1
1
If I have a string like this : and but It still not true because sometimes user wrote like this:. Nothing is clear unless you make it clear!
– Andreas
Nov 20 '18 at 6:42
If I have a string like this : and but It still not true because sometimes user wrote like this:. Nothing is clear unless you make it clear!
– Andreas
Nov 20 '18 at 6:42
Okey, thanks. Sorry.
– Fadly Dzil
Nov 20 '18 at 6:46
Okey, thanks. Sorry.
– Fadly Dzil
Nov 20 '18 at 6:46
add a comment |
You can use this regex, where except TAX part, I have made everything that comes in middle between tax and numbers as optional. Use this regex and capture group1
/tax(?:.*?)s+([d.]+)/i
Demo
Once you get digit and dot part, remove all dot characters from it.
Here is a sample php code,
$arr = ["TAX ID NO. 01.000.589.0.055.000","TAX ID. 01.000.589.0.055.000","TAX ID : 01.000.589.0.055.000","TAX ID 01.000.589.0.055.000","TAX: 01.000.589.0.055.000", "TAX ID NO. 010005890055000"];
foreach ($arr as &$value) {
preg_match('/tax(?:.*?)s+([d.]+)/i', $value, $matches);
echo $value . " --> " . preg_replace('/./','',$matches[1]) . "n";
}
This prints following output,
TAX ID NO. 01.000.589.0.055.000 --> 010005890055000
TAX ID. 01.000.589.0.055.000 --> 010005890055000
TAX ID : 01.000.589.0.055.000 --> 010005890055000
TAX ID 01.000.589.0.055.000 --> 010005890055000
TAX: 01.000.589.0.055.000 --> 010005890055000
TAX ID NO. 010005890055000 --> 010005890055000
1
I believe this one could work even with the new string
– Andreas
Nov 20 '18 at 6:40
add a comment |
You can use this regex, where except TAX part, I have made everything that comes in middle between tax and numbers as optional. Use this regex and capture group1
/tax(?:.*?)s+([d.]+)/i
Demo
Once you get digit and dot part, remove all dot characters from it.
Here is a sample php code,
$arr = ["TAX ID NO. 01.000.589.0.055.000","TAX ID. 01.000.589.0.055.000","TAX ID : 01.000.589.0.055.000","TAX ID 01.000.589.0.055.000","TAX: 01.000.589.0.055.000", "TAX ID NO. 010005890055000"];
foreach ($arr as &$value) {
preg_match('/tax(?:.*?)s+([d.]+)/i', $value, $matches);
echo $value . " --> " . preg_replace('/./','',$matches[1]) . "n";
}
This prints following output,
TAX ID NO. 01.000.589.0.055.000 --> 010005890055000
TAX ID. 01.000.589.0.055.000 --> 010005890055000
TAX ID : 01.000.589.0.055.000 --> 010005890055000
TAX ID 01.000.589.0.055.000 --> 010005890055000
TAX: 01.000.589.0.055.000 --> 010005890055000
TAX ID NO. 010005890055000 --> 010005890055000
1
I believe this one could work even with the new string
– Andreas
Nov 20 '18 at 6:40
add a comment |
You can use this regex, where except TAX part, I have made everything that comes in middle between tax and numbers as optional. Use this regex and capture group1
/tax(?:.*?)s+([d.]+)/i
Demo
Once you get digit and dot part, remove all dot characters from it.
Here is a sample php code,
$arr = ["TAX ID NO. 01.000.589.0.055.000","TAX ID. 01.000.589.0.055.000","TAX ID : 01.000.589.0.055.000","TAX ID 01.000.589.0.055.000","TAX: 01.000.589.0.055.000", "TAX ID NO. 010005890055000"];
foreach ($arr as &$value) {
preg_match('/tax(?:.*?)s+([d.]+)/i', $value, $matches);
echo $value . " --> " . preg_replace('/./','',$matches[1]) . "n";
}
This prints following output,
TAX ID NO. 01.000.589.0.055.000 --> 010005890055000
TAX ID. 01.000.589.0.055.000 --> 010005890055000
TAX ID : 01.000.589.0.055.000 --> 010005890055000
TAX ID 01.000.589.0.055.000 --> 010005890055000
TAX: 01.000.589.0.055.000 --> 010005890055000
TAX ID NO. 010005890055000 --> 010005890055000
You can use this regex, where except TAX part, I have made everything that comes in middle between tax and numbers as optional. Use this regex and capture group1
/tax(?:.*?)s+([d.]+)/i
Demo
Once you get digit and dot part, remove all dot characters from it.
Here is a sample php code,
$arr = ["TAX ID NO. 01.000.589.0.055.000","TAX ID. 01.000.589.0.055.000","TAX ID : 01.000.589.0.055.000","TAX ID 01.000.589.0.055.000","TAX: 01.000.589.0.055.000", "TAX ID NO. 010005890055000"];
foreach ($arr as &$value) {
preg_match('/tax(?:.*?)s+([d.]+)/i', $value, $matches);
echo $value . " --> " . preg_replace('/./','',$matches[1]) . "n";
}
This prints following output,
TAX ID NO. 01.000.589.0.055.000 --> 010005890055000
TAX ID. 01.000.589.0.055.000 --> 010005890055000
TAX ID : 01.000.589.0.055.000 --> 010005890055000
TAX ID 01.000.589.0.055.000 --> 010005890055000
TAX: 01.000.589.0.055.000 --> 010005890055000
TAX ID NO. 010005890055000 --> 010005890055000
edited Nov 20 '18 at 6:59
answered Nov 20 '18 at 6:38
Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi
8,2702927
8,2702927
1
I believe this one could work even with the new string
– Andreas
Nov 20 '18 at 6:40
add a comment |
1
I believe this one could work even with the new string
– Andreas
Nov 20 '18 at 6:40
1
1
I believe this one could work even with the new string
– Andreas
Nov 20 '18 at 6:40
I believe this one could work even with the new string
– Andreas
Nov 20 '18 at 6:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387322%2fphp-regex-to-get-integer-value-that-formatted-separated-by-character%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown