Get to display last URL characters

Asked

Viewed 42 times

0

I need to take the last 14 characters of a relation of urls inside a . txt

$url = file_get_contents('https://www.site.com.br/relacao.txt');

In relation.txt I have:

www.site.com.br/aluno/s/francisco-augusto/11111111111111
www.site.com.br/aluno/s/francisco-fernandes/11111111111112
www.site.com.br/aluno/s/francisco-ocario/11111111111113
www.site.com.br/aluno/s/giselia-augusto/11111111111114
www.site.com.br/aluno/s/giselia-fernandes/11111111111115

Etc.. (várias outras)

And I would like to put it as follows: (only with the latest figures)

11111111111111
11111111111112
11111111111113
11111111111114
11111111111115

Etc...

I’ve tried so many ways.

$url = preg_replace("/[^0-9]/", " ", $url);

But the result is as follows:

11111111111111 11111111111112 1111111111113 11111111111114 etc.

I need each number on one line.

Can someone help me?

1 answer

1


Edit

As seen, your result is not as written in the question as you are doing wrong reading the file.

Try it this way:

$arq = file("url.txt");
foreach($arq as $linha){
   echo buscaFinal($linha);
   echo '<br>';
}

function buscaFinal($linha) {
   $var = substr($linha,(strlen($linha)-14),strlen($linha));
   return $var;
}

You can use the substr with strlen:

$txt = "www.site.com.br/aluno/s/giselia-augusto/11111111111114";
$var = substr($txt,(strlen($txt)-14),strlen($txt));
echo $var;

Exit: 11111111111114

(Obs: you put 12 characters, but in the example has 14)

To give the line break, you can use the php_eol, echo <br>, echo '\n', it depends on whether to print to the file, on the page, etc

function buscaFinal($linha) {
  $var = substr($linha,(strlen($linha)-14),strlen($linha));
  echo $var;
  echo '<br>';
}

Other form standard case the bars "/"

You can use the explode:

$txt = "www.site.com.br/aluno/s/giselia-fernandes/11111111111115";
$var = explode("/", $txt);
echo $var[4];

Exit: 11111111111115


Documentations:

explode

replace

strlen

Predefined Constants

  • Function buscaFinal($line) { $var = substr($line,(strlen($line)-14),strlen($line); echo $var; php_eol; } $url = file_get_contents('https://www.site.com.br/relaca.txt'); $number = searchFinal($url); echo $numero; Returns only the first number of the relation.

  • But then you have to read the txt and go playing the line you are reading. I considered that you are already reading your txt. Click on edit in your question and post all your code, so I can make an example in it.

  • So, I have a.txt relationship with thousands of urls inside. (as stated in the question) I need to access the.txt relation get all urls and then print ONLY the last 14 characters. per line (as mentioned in the question)

  • But you say "But the result is as follows:", so it is understood that you are already accessing the txt and have it printed correctly? Then post the part that reads the txt, and the loop that you use to print the content. It is not the txt content that is to post.

  • I don’t think I understand... $url = file_get_contents('https://www.site.com.br/relacao.txt'); // here do I access the right content? if I echo at $url I have: www.site.com.br/student/s/francisco-August/11111111111111 (and several other urls) I need: when printing the result it just shows (of all urls) the last 14 numbers.

  • So your return is not 11111111111111 11111111111112 1111111111113 11111111111114 as in the question. This is not how you use the file_get_contents.

  • I will open a chat if you need to continue the issue, as it has already extended the comments.

Show 2 more comments

Browser other questions tagged

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