Reputation: 408
I have this total USD100.75 from
string.I am getting the next word like this
$s = 'total USD100.75 from';
$r = preg_match_all('/(?<=(total))(\s\w*)/',$s,$matches);
print_r($matches[0]);
However i would like to get the string between two specific strings i.e total
and from
. How may i get the middle string?.
Upvotes: 0
Views: 92
Reputation: 5661
I'm not sure of the specifics of your language, but in Java:
(?USD[0-9]+\.[0-9]{2})
would work.
If you have commas, you could try:
(?USD[0-9]{1,3}(,?[0-9]{3})*\.[0-9]{2})
Upvotes: 1
Reputation:
@user3272483, Please check below answer for your solution.
$matches = array();
preg_match("/total (.*?) from/", 'total USD100.75 from', $matches);
print_r($matches);
Upvotes: 0
Reputation: 1169
The regular expressions are easy way to do it. You just need to understand the basics. You will achieve what you want with the following pattern:
total\s(.*)\sfrom
Upvotes: 0
Reputation: 2572
Using substr
and strlen
:
echo substr($string, strlen("total "), -strlen(" from"));
Upvotes: 1
Reputation: 18600
You can explode your string by space.
$s = 'total USD100.75 from';
$s1 = explode(" ", $s);
echo $s1[1];
Output
USD100.75
Upvotes: 0