Jump to content

How to repeat something with small variation?


typeslowly

Recommended Posts

$url = "http://www.example.com/1;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
curl_close($ch);

$regex = '/[v][0-9]{5}[A-Za-z0-9]{8}/';
preg_match($regex,$page,$match);
var_dump($match);
echo $match[1];

 

Hi, I'm trying to make this code repeat with the number at the end of the URL changing every time. How do I set a a range of numbers? What would be the best way to store all this data? Sorry if this a dumb question, I tried searching and got nothing.

Link to comment
Share on other sites

try

<?php

$numbers = range (1, 50);

foreach ($numbers as $n)
{
    $url = "http://www.example.com/$n";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $page = curl_exec($ch);
    curl_close($ch);

    $regex = '/[v][0-9]{5}[A-Za-z0-9]{8}/';
    preg_match($regex,$page,$match);
    var_dump($match);
    echo $match[1];
}
?>

Link to comment
Share on other sites

That works great, thanks. But it comes out really weird(to me)

array(1) { [0]=>  string(14) "v554652GWNXwwR" 
} Arrayarray(1) { [0]=>  string(14) "v554657ky4CDT8"
} Arrayarray(1) { [0]=>  string(14) "v554654a826KZG"
} Arrayarray(1) { [0]=>  string(14) "v554653QzA7C5p"
} Arrayarray(1) { [0]=>  string(14) "v5546566Jr9zsY
" } Arrayarray(1) { [0]=>  string(14) "v554655JnBWd4x
" } Arrayarray(1) { [0]=>  string(14) "v554658Ybj4xKY"
} Arrayarray(1) { [0]=>  string(14) "v55465992CnjqC"
} Arrayarray(1) { [0]=>  string(14) "v554660NAX6bDR"
} Arrayarray(1) { [0]=>  string(14) "v554661sA79eqp"
} Arrayarray(1) { [0]=>  string(14) "v554662MF8Gyy6
" } Arrayarray(1) { [0]=>  string(14) "v554663FpMNGHc
" } Arrayarray(1) { [0]=>  string(14) "v10514612RdCCk
" } Arrayarray(1) { [0]=>  string(14) "v554670N34jke7
" } Arrayarray(1) { [0]=>  string(14) "v5546718DyMZpw
" } Arrayarray(1) { [0]=>  string(14) "v5546723QPaM4N"
} Arrayarray(1) { [0]=>  string(14) "v554673JhwskMJ" 
} Arrayarray(1) { [0]=>  string(14) "v962240SBK3REq" }
Arrayarray(1) { [0]=>  string(14) "v554666QWtbAAZ" }
Arrayarray(1) { [0]=>  string(14) "v554668dByX7Hx" } 
Arrayarray(1) { [0]=>  string(14) "v554664BaSCKEE" } 
Arrayarray(1) { [0]=>  string(14) "v554676m4EQxze" }
Arrayarray(1) { [0]=>  string(14) "v554667mKppgRN" }
Arrayarray(1) { [0]=>  string(14) "v554674rEXyj87" }
Array

How can I get all of those into a .csv file or something so I can manipulate them easier? I don't even know how to ask the question, haha. If you could give some general topics I could read about this, that would be cool.  How do you make your code colored>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.