I am trying to create a regex for a currency in USD starting with any number of digits followed by 2 digits following a decimal point followed by a digits within round brackets. I want just the part before the round brackets. Like $179.29 (3). Output should be $179.29.
Asked
Active
Viewed 88 times
-1
-
These litter the internet, have you not see one already? – bren Jul 21 '16 at 23:29
1 Answers
1
You don't need a regex for this. You can just explode on the space and take the first piece.
$string = '$179.29 (3)';
$output = explode(' ', $string)[0];

Don't Panic
- 41,125
- 10
- 61
- 80
-
1
-
Thank you for your answer. I tried that and I'm getting the below warning : – user6622569 Jul 21 '16 at 23:39
-
PHP Warning: explode() expects parameter 2 to be string, object given in /scraper/api/neweggApi.php on line 70 NULL – user6622569 Jul 21 '16 at 23:39
-
Alas, @user6622569, it seems there is more to your problem than you initially disclosed. You know, you will run into exactly the same problem if you try a regex instead. – Don't Panic Jul 21 '16 at 23:41
-
Both regex and this method are intended to work on strings, and it sounds like you're feeding it some kind of object, but what that object actually may be is not specified in your question. – Don't Panic Jul 21 '16 at 23:43
-
I'm trying to get DOM elements from a website something like getting the childnodes of an element and then fetching $result=$childNode-> item(x), if(isset($result)){ $finalResult = "Here comes the part where Im trying to get the price" } – user6622569 Jul 21 '16 at 23:45
-
It sounds like you just need to get the inner text of the node before you try to remove the parenthetical digits. I don't know what dom parser you're using, but maybe [like this](http://stackoverflow.com/questions/17054815/php-domdocument-read-element-inner-text)? – Don't Panic Jul 21 '16 at 23:47
-
Thank you so much. I tried that but I'm getting an empty string now in my output. – user6622569 Jul 21 '16 at 23:56
-
string(190) " $179.00 (3 Offers) – This is my string and output is string(2) " " – user6622569 Jul 21 '16 at 23:58
-
It looks like the string has some space at the beginning. Try trimming it before you explode it. – Don't Panic Jul 22 '16 at 00:29
-
Thanks i figure it out. I changed the delimiter and it worked fine for me. – user6622569 Jul 22 '16 at 00:33