Why is my PREG_MATCH not working?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Why is my PREG_MATCH not working?

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my PREG_MATCH not working?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why is my PREG_MATCH not working?

Post by Celauran »

You're looking to match D11H(anything here)= ?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why is my PREG_MATCH not working?

Post by Celauran »

Take a look at Rubular: http://rubular.com/r/YJ2Y5CCxJI
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my PREG_MATCH not working?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why is my PREG_MATCH not working?

Post by Celauran »

What is $row2->romancode and what are you trying to match against?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my PREG_MATCH not working?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why is my PREG_MATCH not working?

Post by Celauran »

Looks like it should be working. Hard to test without values.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my PREG_MATCH not working?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why is my PREG_MATCH not working?

Post 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]
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why is my PREG_MATCH not working?

Post by simonmlewis »

Code: Select all

 if (preg_match("/$row2->romancode.*?=/i", $contents))
Bingo - this works!
The difference between ' and " is amazing. But why?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply