Page 1 of 1

Why is my PREG_MATCH not working?

Posted: Tue Sep 29, 2015 8:43 am
by simonmlewis

Code: Select all

$romanurl = "https://sub.site.com/data.asp?storeid=44444&categoryid=$rows->romancartcatid";
                $contents = file_get_contents($romanurl);
                if (preg_match("$row2->romancode", $contents))
                {
                echo "Correct code";
                }
If I go to the proper full URL of $romanurl, I get a large batch of code on screen, including many stock codes, always in UPPERCASE.

I want to check if $row2->romancode is within that batch of code. I thought using this script would work. But preg_match just isn't working. Am I doing it work, with the quotes or anything like that?

Re: Why is my PREG_MATCH not working?

Posted: Tue Sep 29, 2015 9:01 am
by simonmlewis
I now see where I was going wrong:

Code: Select all

if (preg_match('/$row2->romancode/i', $contents))
But now I have noticed in the batch of code it's search, there are many with similar names with extra characters on the end.

ie D11H, and D11HBLUE.

However, each one ends with a =. So D11H='9.99'.

So how do I add the = into my preg_match, so it's looking for the code + the =.
I have used this before:

Code: Select all

preg_match('/var cText = \'([0-9]+)\'/', $contents, $matches);
To find var cText = 5, for example. But I don't know how to flip this new version around, wtih the = AFTER the variable.

Re: Why is my PREG_MATCH not working?

Posted: Tue Sep 29, 2015 9:44 am
by Celauran
You're looking to match D11H(anything here)= ?

Re: Why is my PREG_MATCH not working?

Posted: Tue Sep 29, 2015 9:46 am
by Celauran
Take a look at Rubular: http://rubular.com/r/YJ2Y5CCxJI

Re: Why is my PREG_MATCH not working?

Posted: Tue Sep 29, 2015 9:51 am
by simonmlewis
Sorry not quite with you on that page.
I've tried something and I think this is what I got:
if (preg_match('/$row2->romancode.*?=/i', $contents))
But it doesn't work as everything is coming up as incorrect, and 99% should be correct.

Re: Why is my PREG_MATCH not working?

Posted: Tue Sep 29, 2015 10:14 am
by Celauran
What is $row2->romancode and what are you trying to match against?

Re: Why is my PREG_MATCH not working?

Posted: Tue Sep 29, 2015 10:15 am
by simonmlewis
That is the stock code, such as D11HBLUE or D11H. And I'm trying to see if that, with a = on the end, is stored within the $contents variable that has a bunch of characters in it.

Re: Why is my PREG_MATCH not working?

Posted: Tue Sep 29, 2015 10:30 am
by Celauran
Looks like it should be working. Hard to test without values.

Re: Why is my PREG_MATCH not working?

Posted: Tue Sep 29, 2015 10:37 am
by simonmlewis
Within that $contents, is tons of text from a database.
A brief example of which is: "Email Me When Back in Stock</a>';var roc_rdd_available_D92H='Available to order';var roc_rdd_notavailable_D92H='';var roc_rdd_qp1_D92H='N".

So I need to know if a stock code is in there. If D92H also has one called D92HBLUE, then querying D92H will work, even if D92H is NOT there.... coz the one with BLUE will be there.

However, each one has at least one entry where there is a = after it's code. And that is what I need to find.

I know it's failing because I am getting the error of that it has NOT found the code, yet I can see it in the bunch.

I'm not permitted to provide all the code the site gives for privacy reasons, but whatever... it's not really working. As it's saying none are in there.

Re: Why is my PREG_MATCH not working?

Posted: Tue Sep 29, 2015 10:38 am
by Celauran
There's also the question of single quotes and string interpolation
[text][11:36 am] [~]
$ php -a
Interactive shell

php > $code = 'B11H';
php > $string = 'This is a long string that B11H-AB=9.99 contains the value above.';
php > var_dump(preg_match('/$code.*?=/i', $string));
int(0)
php > var_dump(preg_match("/{$code}.*?=/i", $string));
int(1)
php >[/text]

Re: Why is my PREG_MATCH not working?

Posted: Tue Sep 29, 2015 10:46 am
by simonmlewis

Code: Select all

 if (preg_match("/$row2->romancode.*?=/i", $contents))
Bingo - this works!
The difference between ' and " is amazing. But why?

Re: Why is my PREG_MATCH not working?

Posted: Tue Sep 29, 2015 10:49 am
by Celauran