In my test there is following data
>0 Dollar</span
>0.01 Dollar</span
>0.00 Dollar</span
>50.00 Dollar</span
My desire:
I want to differentiate dollar which is not 0.00 Dollar and 0 Dollar
Code that I am using
$str = $table['contents'];
$pattern = "/(Need help here)/";
$a = preg_match_all($pattern, $str, $matches);
print_r($matches);
Output should be an array whose value will be:
0.01 Dollar and 50.00 Dollar
Could you help me?
2
Answers
You want to use the Lookaround, specially negative lookahead.
Output:
You could make use of DOMDocument and DOMXPath and use preg_match as a PhpFunction inside a xpath query.
In the example I have used
//span
which will get all the spans, but you can make the query more specific to your data.Output
See a PHP demo
If you want to use a regex only, you could match the leading
>
and assert the trailing<
. If the previous code example,A
andz
are anchors that assert the start and the end of the string.The pattern matches:
>
Match literallyK
Forget what is matched so far(?=[0.]*[1-9])
Positive lookahead, assert at least a digit 1-9 preceded by optional zeroes or dotsd+(?:.d+)?+
Match 1+ digits with an optional decimal parth+Dollar
(?=<)
Positive lookahead, assert<
to the rightRegex demo | Php demo
For example:
Output