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.

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];
}
?>

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>

var_dump() can be bit cluttered. Clearer if you put the output between "pre" tags

 

echo '<pre>', print_r($match, true), '</pre>';

 

To get php code color-coded, put "<?php" at the start of the code

 

<?php
echo '<pre>', print_r($match, true), '</pre>';

 

See

www.php.net/fputcsv

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.