On that line of your code $url = str_replace(' ','',$url);
returns a Random url of one of the files txt
of the directory pasta
as an example www.site.com/nome/juliana.html
Right after you use file_get_contents;
whose purpose is to read the contents of a file. Only you are trying to read the contents of a url file_get_contents($url);
since $url
returns for example www.site.com/nome/juliana.html
and this will only generate an error and nothing more.
Warning: file_get_contents(www.site.com/nome/juliana.html):
failed to open stream: No such file or directory in .....
So remove that line from your code that serves nothing but generating one Warning
.
If you already have a url to redirect, I don’t understand why the list of urls
<li class="ohyeah">
.............
.............
</li>
You should have your reasons there and therefore to do this you can use the code below.
$conteudoLista="";
$arr="";
$i=1;
$pasta = 'pasta/';
$diretorio = dir($pasta);
while($arquivo = $diretorio -> read()){
if (file_exists($pasta.$i.".txt")) {
$f = file($pasta.$i.".txt");
foreach($f as $item){
$arr .= $item .",";
$strDir = str_replace(".html","",$item);
$nome = substr($strDir, 18);
$conteudoLista .="<a href='http://".$item."'>".$nome."</a><br>";
$conteudoLista = preg_replace( "/\r|\n/", "", $conteudoLista );
}
}
$i=$i+1;
}
$diretorio -> close();
$enderecos = (substr($arr,0,-1));
$partes = explode(',',$enderecos);
$qtd = count($partes);
$numRnad = (rand(1,$qtd));
$result=$partes[$numRnad];
echo "<li class=\"ohyeah\">".$conteudoLista."</li>";
echo "<br>";
echo "link random : ".$result;
Explaining:
$pasta = 'pasta/'
- directory relative path
dir()
- With this function, we will read the folder directory and, using the read() method, we will list all the existing files in this directory.
$diretorio = dir($pasta)
- read the directory
while($arquivo = $diretorio -> read())
- iteration to go through the files by listing them with the read method()
if (file_exists($pasta.$i.".txt"))
- make a check to know if the file exists
foreach($f as $item)
- iteration in which $item receives the value of each array item
$arr .= $item .","
- concatenating each item separated by comma
str_replace
- to remove (replace) the . html extension from the urls
substr($strDir, 18)
- Extract a part of a string - returns the part of the url from position 18 to the end, i.e., eliminates - www.site.com/name/ - which is common to all urls
preg_replace( "/\r|\n/", "", $conteudoLista )
- performs a search for a regular expression and replaces it - n line break and r car return
substr($arr,0,-1)
- removing the last $arr comma
$partes = explode(',',$enderecos)
- Let’s separate the $addresses string in all comma occurrences
$qtd = count($partes)
- get the number of elements that make up the $parts array
$numRnad = (rand(1,$qtd))
- generates a random number between 1 and the number of elements in the $parts array
$result=$partes[$numRnad]
- returns the value of the $numRnad position of the $parts array
$diretorio -> close()
- close the reading
Documentation used
What’s the point of this? Why so many redirects?
– Woss
Play everything for an array, and then do an array_rand
– Don't Panic
@Andersoncarloswoss, for a URL sharing system. The content of the site is not saved in a database so for me to capture the urls the way I need to... it’s the easiest way I could find.
– Marcelo
@Everson, I don’t know enough. Can you help me?
– Marcelo
I’ll edit the question to make it easier. A moment.
– Marcelo
Put html content with urls here to help you. Edit your Question to keep cohesion !
– Rafael Salomão
@Rafaelsalomão, made!
– Marcelo
Honestly, I still don’t understand. All the files
.txt
have a URL, and within each URL has a reference to a.html
and in this HTML, they all have the tag<li class="ohyeah">
?– Don't Panic
@Everson, exact. I already get the URL inside the txt file. Now I need to search A single url inside the <li tag and redirect the user to it.
– Marcelo