I have the following PHP code:
$linea = "Fuente: https://www.unsitioweb.com";
$datos = explode(" ", substr($linea, 0,7));
$https = "https://";
$encontrado = false;
foreach ($datos as $protocolo) {
if ($protocolo == $https) {
$encontrado = true;
}
}
if ($encontrado) {echo "<a href='$linea'>Link encontrado</a>";}
else {echo "Sin protocolo";}
In theory, the function that it should fulfill is that if at any time it finds " https " it will be converted to a label with its respective link. In this way:
<a href="https://www.unsitioweb.com">Link encontrado</a>
But it is not working for me, if I am doing something wrong or there is a more effective way to do it I would appreciate your answers.
You can try the following code, what I changed was the foreach for a strpos. The strpos() function finds the position of the first occurrence of a string within another string.
The main error is in this line:
That if you pay attention, you first shorten the string seven characters and then separate it by spaces
Solution:
What you could do is:
eg $link = explode(" ", $linea)[1];
)eg: $protocol = substr($link, 0, 8);
)eg: if ($protocol == $https)
)Example:
PS: You could save yourself doing
$link = explode(" ", $linea)[1];
and directly do$link = substr($linea, 8);