Pick site name by link

Asked

Viewed 318 times

0

//  IRIA PUXAR DO BANCO DE DADOS OS LINKS, MAS ASSIM JÁ DA UM EXEMPLO DE COMO IRIA FICAR.

$listalinks = "http://site1.com/embed1/CODIGO http://site2.com/embed2/CODIGO http://site3.com/embed3/CODIGO http://site4.com/embed4/CODIGO"; 

function pegaListadeUrlDaBase() {
 

   // Exemplo com links em uma única string da base, separados por ' ';
   $links_text = "$listalinks";
   return explode(" ", $links_text);
}

$urls = pegaListadeUrlDaBase();


foreach($urls as $url) {
   $nome = explode('.', parse_url($url, PHP_URL_HOST))[0];
   echo "<a href='$url'>$nome</a>";
}

I put it like this and it won’t catch.

  • 1

    what’s the idea, what you need, I didn’t understand your question.

  • 2

    I didn’t understand anything

  • 2

    how so "register these links on the server"?

  • wanted to register in the database the first part there, and with php returned the second part there, but only taking the first parts of the url that is site1,site2,site3,site4, without the . with, and formatting leaving the way I posted there in the second part creating a link with the first part of the site url

  • Possible duplicate of How to get the site name?

3 answers

1

I think this will do for you:

index php.

<form id="form" name="form" method="post" action="envia.php">
   Links: <label for="nome"></label>
          <textarea name="nome" id="nome" cols="20" rows="8"></textarea>
          <input type="submit" name="button" id="button" value="Enviar" />
</form>

<?php
include("conexao.php");

$sql = "SELECT seu_campo FROM sua_tabela";

$result = mysqli_query($conn, $sql);

while ($linha = mysqli_fetch_assoc($result)){

  echo "<a href=".$linha['nome'].">".$linha['nome']."</a><br>";
}
?>

In the index.php, you enter the values in the textarea and then they are shown as link below, after insertion.

php.

<?php
include_once("conexao.php");

$nomes = $_POST['nome'];
$separar = explode("\n", $nomes);

foreach($separar as $nome){
    $query = "INSERT INTO sua_tabela (seu_campo) VALUES ('".$nome."')";
    $resultado = mysqli_query($conn, $query);
}

echo "Seu cadastro foi realizado com sucesso!";

?>

This is where it does all the separating magic by line breaking using the explode and then you can enter it into the database.

php connection.

<?php
  $servidor = "localhost";
  $usuario = "root";
  $senha = "1234";
  $dbname = "teste";

  $conn = mysqli_connect($servidor, $usuario, $senha, $dbname);

?>

And this is the connection file I used, just exchange the value of the variables for your mysql data.

Finally, replace where it says "your table" and "your field" and I believe it will work smoothly.

1

You can use php’s native function parse_url to retrieve information from a URL. That way:

$listalinks = "http://site1.com/embed1/CODIGO http://site2.com/embed2/CODIGO http://site3.com/embed3/CODIGO http://site4.com/embed4/CODIGO";

$urls = explode(" ", $listalinks);


foreach($urls as $url) {
   $nome = explode('.', parse_url($url, PHP_URL_HOST))[0];
   echo "<a href='$url'>$nome</a>";
}

This example will print:

<a href='http://site1.com/embed1/CODIGO'>site1</a>
<a href='http://site2.com/embed2/CODIGO'>site2</a>
<a href='http://site3.com/embed3/CODIGO'>site3</a>
<a href='http://site4.com/embed4/CODIGO'>site4</a>

View documentation:

http://php.net/manual/en/function.parse-url.php

http://php.net/manual/en/function.explode.php

  • I wanted to register in the database all over, and show with php the second part, but taking only the first part of the links that would be the site1,site,2,site,3,site4 without the . with, leaving as it is in the second part there, knows a solution?

  • Amigo.. you will simply return to the base’s URL list and apply the code.. I updated the reply to show the result exactly as you requested with <a href...

  • That’s exactly what I wanted, but in case the url are all saved together, in the example there in Return, are separate, how would I be able to do that? I don’t understand very well of php

  • if you give an example of how it gets saved really I can give you a solution friend.

  • I have an edge that registers links, and has a textarea called LINKS, but instead of me registering one link at a time, I place all links at once http://site1.com/embed1/CODIGO http://site.com/embed2/CODIGO http://site3.com/embed3/CODIGO http://site4.com/embed4/CODIGO &#Xa?

  • friend.. if the links are in a single string separated by a space. you can give a explode(" ", $string_de_urls) before to return an array with separate links.

  • I edited the answer for an example where the links are in a single string. If it doesn’t work, you could update your question with the format exactly as it is in your database so I can use.

  • Can you help me one more time? I’m trying to pull the string $listalinks, putting it inside $links_text ="$listalinks"; and it’s not pulling anything, you know what it is? if I put it on any other part of the site, it’s appearing, but there it doesn’t seem to be

  • $listlinks is where stores links

  • Update your question with the code, otherwise it becomes difficult for me to help without seeing.

  • Updated, has how to give a strength?

  • blz friend.. the method I created there was an example of how it would be fetching from the base and returning the list.. but since you have these values saved.. you can simply remove the method. and do $urls = explode(" ", $listalinks);

  • You got 100%, thank you!!!

Show 8 more comments

-1

$ssite=('http://site.com'); 

$q=@file_get_contents($ssite); 

preg_match_all('#<title>([^<\/]{1,})<\/title>#i',$q,$match);

$otitle=($match[1][0]); 

echo $otitle; // Mostra frase do title

Browser other questions tagged

You are not signed in. Login or sign up in order to post.