How can I preg_match until no more results is found?
I'm using curl to login a page and then delete posts from there.
But to delete those posts I need to preg_match the content and filter the IDs and if found ids there my script run the delete command.
So, basically:
$pattern = '/(?<=list_id=).*?(?=&cmd=edit)/s';
preg_match($pattern, $LoginResult, $id); //THIS PREG_MATCH IS WORKING, IT GETS THE FIRST RESULT OF THE PAGE (WHAT I NEED). BUT I NEED TO MAKE A LOOP TO THIS SCRIPT RUN OVER AND OVER UNTIL NOTHING MORE IS FOUND.
$idpagina = $id[0];
In words it should make something like:
With this code I can find everything there is between list_id= and &cmd=edit. If the script find something between this two strings, It needs to perform a curl to delete this ID:
//THIS IS WORKING
$paginadelete = "https://example/list/folder/0?list_id=".$idpagina."&cmd=delete&type=AD_DELETE";
curl_setopt($login, CURLOPT_URL, $paginadelete);
curl_setopt($login, CURLOPT_POST, 1);
curl_setopt($login, CURLOPT_FOLLOWLOCATION, 1);
$step1 = curl_exec($login);
echo $step1;
What this basically does is (or should do):
But this script run 3 curl processes:
So this loop should be between step #2 and #3 until nothing more is found.
My #3 step (confirm delete) is this one:
$url = curl_getinfo($login, CURLINFO_EFFECTIVE_URL);
$url = parse_url($url, PHP_URL_PATH);
$url = substr($url, 9);
$url = "http://example.com/cmd/act/".$url;
$post_data = array(
'1' => 'delete',
'2' => '1',
'3' => '2',
'4' => '10',
'5' => '',
'6' => 'continue',
);
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
curl_setopt($login, CURLOPT_URL, $url);
curl_setopt($login, CURLOPT_POST, 1);
curl_setopt($login, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($login, CURLOPT_POSTFIELDS, $post_string);
$step2 = curl_exec($login);
//echo $step2;
////////////////////// EDIT
I was trying:
if (preg_match('/(?<=list_id=).*?(?=&cmd=edit)/s', $LoginResult, $id)){
}
else {
}
But this will only work for the first result. After that, the script stops. I need to re-run the if until preg_match is false and then end in the else.
I thought about using DO and WHILE, but I don't know how and neither if it'll work.
////////////////// EDIT 2
I'm now trying to use a GOTO until get false and close connection
verification:
if (preg_match('/(?<=list_id=).*?(?=&cmd=edit)/s', $LoginResult, $id)){
[..........]
} else {
//close the connection
curl_close($login);
}
goto verification;
But doesn't seem to work, lol.
Based on your first edit, It seems that what you are trying to achieve is the following:
while(preg_match('/(?<=list_id=).*?(?=&cmd=edit)/s', $LoginResult, $id)) {
// do stuff
}
// do stuff after the preg_match is false
Edit:
Based on your description on the comments maybe this code will satisfy your needs.
while(true) {
$result = preg_match('/(?<=list_id=).*?(?=&cmd=edit)/s', $LoginResult, $id);
if($result) {
// Run Delete Curl
} else {
curl_close($login);
break;
}
}