CURL and Preg_match?... help needed

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
suicide
Forum Newbie
Posts: 19
Joined: Wed Jun 14, 2006 10:32 am

CURL and Preg_match?... help needed

Post by suicide »

so I have a credit card processor that suggested i use CURL to see if the transactions are approved

So I got the Curl part working (I think)


When the use enters his data into my form and presses submit my page uses curl to contact the Credit Card processor and pulls the page int oa variable

the pages datat looks like this

Code: Select all

<html><head><title>Response File Redirect</title></head><body onload="document.myForm.submit()"><form action="
https://gg.com/vposwebreponse.asp" method="POST" name="myForm"><input type="hidden" name="Transactionamount" value="1.00"><input type="hidden" name="Orderstring" value="4~Resients~1.00~1~N~||"></form></body></html>

The question is... i need several of those values to figure out what my script does next...
mainly this one

Code: Select all

<input type="hidden" name="szIsApproved" value="1">
if it is a 1 i need to do something, otheriwse the creidt card or info was junk


so i woudl love to do a $szIsApproved = pregmatch(ihavenoclue);

for all the impt variable


but how do i fish that data out of there for different variables? so i can do a

switch szIsApproved (or whatever vairable im getting info for)
case 1:
do this
case 0:
do this then



MANY tHanks in advance.... jst tell me wher eto send the beer.
Last edited by suicide on Wed Oct 25, 2006 8:24 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Generic response: What have you tried?
suicide
Forum Newbie
Posts: 19
Joined: Wed Jun 14, 2006 10:32 am

Post by suicide »

Code: Select all

$ret_data = curl_exec ($ch);
		preg_match('/szIsApproved=(-*[0-9]*)/i', $ret_data, $arr);
		
		switch ($arr[1])
		{
			case '1':

then do everything i want;
break;

case '0':
echo $error
break;
}

The problem is that it only sometime works like this, which has been ripping my head apart

and i need to support more then just the IsApproved, so if i coudl break down each value i need into a variable first and then go from there, I figured that would be the best shot.


no matter how much i read about reg expression i cant make sense of this stuff in my head, so if someone wants to try and explain that would be great too (teach a man to fish and all)

thansk again for your time
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Perhaps something on the order of

Code: Select all

preg_match('~<input[^>]*szIsApproved[^>]*value\s*=\s*["\']?\s*(-?\d+)~ims', $ret_data, $arr);
If it works, I would be happy to explain it.

edit: just noticed an unescaped single quote in the single quoted string. Makes me wish regex were first class citizens in PHP :(
Last edited by sweatje on Tue Aug 29, 2006 9:14 am, edited 2 times in total.
suicide
Forum Newbie
Posts: 19
Joined: Wed Jun 14, 2006 10:32 am

Post by suicide »

holy hell that thing is scary
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Did it work?
suicide
Forum Newbie
Posts: 19
Joined: Wed Jun 14, 2006 10:32 am

Post by suicide »

so far so good :)

I wont get to test it in teh live enviroment till i get home tonight (ip restricted ftp)

But I set up

Code: Select all

<?php
$ret_data = '<html><head><title></form></body></html>';

//preg_match('/szIsApproved=(-*[0-9]*)/i', $ret_data, $arr); 
preg_match('~<input[^>]*szIsApproved[^>]*value\s*=\s*["\']?\s*(-?\d+)~ims', $ret_data, $arr);

echo $arr[1];
?>
and it retruned a 1...


so care to explain how the hell that monster works?
:)
Last edited by suicide on Wed Aug 30, 2006 10:35 am, edited 1 time in total.
suicide
Forum Newbie
Posts: 19
Joined: Wed Jun 14, 2006 10:32 am

Post by suicide »

im still playing with it...

is there a way to streamline that statement into assigning variables?

so I can get the value striaght to a variable like (right now this retruns a 1, im assuming that TRUE on the array)

$INTszIsApproved = preg_match('~<input[^>]*szIsApproved[^>]*value\s*=\s*["\']?\s*(-?\d+)~ims', $ret_data, $arr);




or should I just keep essiging differnet tings to arr[1]?
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

suicide wrote: so care to explain how the hell that monster works?
:)
Sure:

'~<input[^>]*szIsApproved[^>]*value\s*=\s*["\']?\s*(-?\d+)~ims'

Code: Select all

'     start a single quoted string
~     use the ~ character for a delimiter, this means the regex will have to end with a ~ and any real ~ would need to be escaped as \~ in the regex
<input     look for the start of an input tag
[^>]*     [] denotes a character class, basically a list of characters to match ^ is anything but, so [^>] means anything but a > character, with the * being a shortcut repition modifier for zero or more
szIsApproved     then the string you are looking for
[^>]*      another of the same, as much as you can grab up to the end of the input tag
value     then looking for the word value
\s*      \s means whitespace, * meaning zero or more, so \s* means "optionally some whitespace"
=      then an = character
\s*      more optional whitespace
["\']?       a character class containg either a single or a double quote, with the ? meaning zero or one, interpret this as "optionally the start of a quoted identifier"  the single quote has to be escaped so PHP doe not end the string there
\s*      more optional whitespace
(      the start of the first match, if preg_match returns true, this will be in $arr[1]
-?      optionally a - character
\d+      one or more digits
)     end of the capture group
~ims      end of the regex, using case insensitivity, multiline and . grabs all modifiers (the last is unnecessary in this regex, but I am in the habit of adding it when I make a regex multiline).
' end the single quoted string
HTH
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

suicide wrote:im still playing with it...

is there a way to streamline that statement into assigning variables?

so I can get the value striaght to a variable like (right now this retruns a 1, im assuming that TRUE on the array)

$INTszIsApproved = preg_match('~<input[^>]*szIsApproved[^>]*value\s*=\s*["\']?\s*(-?\d+)~ims', $ret_data, $arr);




or should I just keep essiging differnet tings to arr[1]?
No, preg_match will return a boolean for if the regex matched. I would suggest a function:

Code: Select all

function getIntFromName($name, $data) {
  if (preg_match('~<input[^>]*'.preg_quote($name,'~').'[^>]*value\s*=\s*["\']?\s*(-?\d+)~ims', $data, $arr)) {
    return $arr[1];
  }
}
suicide
Forum Newbie
Posts: 19
Joined: Wed Jun 14, 2006 10:32 am

Post by suicide »

alright dude, you rule... where do i send the beer?
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

suicide wrote:alright dude, you rule... where do i send the beer?
That is ok, we are here to help.

As someone else's sig around here say "No applause, just throw money". ;) Maybe you can buy a copy of my book instead of buying me beer and kill two birds with one stone :lol:
suicide
Forum Newbie
Posts: 19
Joined: Wed Jun 14, 2006 10:32 am

Post by suicide »

Im still not following what the ~ does... I'll try and figure it tho

That book looks pretty scary too :)
You write anything a little less painful? :)

Thanks again man
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

suicide wrote:Im still not following what the ~ does... I'll try and figure it tho
It is just a delimiter for the re, the start and end character for it. Typically / is the delimiter, but with many PHP expressions you would have to escape the / in end tags, so I just kind of got in the habit of using ~ because I don't run across a use for that specific character in may of the re I write.

suicide wrote: That book looks pretty scary too :)
You write anything a little less painful? :)

Thanks again man
Wrote about a 1/3 of a book on PHP graphics (specifically on using JpGraph for charting) but that one is out of print now.

I have a few power point presentations up on my blog from various PHP conference presentations I have made.

Other than that, just random blog and forum postings.

Oh, and my senior honors thesis from college: "The Application of Neural Networks to the Forecasting of Economic Time Series Data" ;)
Post Reply