I have this long string that I received from using CURL to grab data from a form post. In this long string I need to find the word "Error" and then grab any numbers that come after it. So if during the search it finds "Error 2" I need to grab the 2 and display it. My problem is that when I try to print it out I am getting NULL and an empty array. My code is below.
$Rec_Data = curl_exec($ch);
ob_start();
header("Content-Type: text/html");
$Temp_Output = $Rec_Data;
if(strpos($Temp_Output,"Error")>=0){
preg_match("/Error (\d+)/", $Temp_Output, $error);
var_dump ($error[1]); //prints NULL for $error[0] and $error[1] and when printing $error it is an empty array.
}
Here is my CURL code
$PostVars = "lname=" . $lname . "&fname=". $fname . "&uid=" . $uid . "&rsp=" . $rsp . "&z1=" . $z1 . "&module=" . $module . "&CFID=" . $CFID . "&CFTOKEN=" . $CFTOKEN;
$ch = curl_init(POSTURL);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS , $PostVars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0); // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF THE CALL
$Rec_Data = curl_exec($ch);
var_dump ($Rec_Data);
ob_start();
header("Content-Type: text/html");
$Temp_Output = $Rec_Data;
Maybe 'Error' is not on page. Try
Error\s*(\d*)
If you have string interpolation try
"/Error\\s*(\\d*)/"
Not a php user, but if single quote, try
'/Error\s*(\d*)/'
If its like Perl, the delimeter is the quote
/Error\s*(\d*)/
From the documentation page, strpos
returns false
when nothing is found.
Try using this in your IF
instead:
if(strpos($Temp_Output,"Error") !== false ) {
// Do other things
}
Since false >= 0
evaluates to true in PHP well... you always get into the IF
no matter what.
Disclaimer: I didn't know how strpos
worked prior to posting this answer.