Catch showing last URL characters

0

I need to get the last 14 characters of a urls relationship inside a .txt

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

Within relacao.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 present it as follows: (only with the last numbers)

11111111111111
11111111111112
11111111111113
11111111111114
11111111111115

Etc...

I've tried it in several ways.

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

But the result looks like this:

11111111111111 11111111111112 1111111111113 11111111111114 etc .

I need each number to be on one line.

Can anyone help me?

    
asked by anonymous 16.07.2018 / 20:20

1 answer

1

Edit

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

Try this:

$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 substr with strlen :

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

Output: 11111111111114

(obs: You have placed 12 characters, but in the example you have 14)

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

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

Another way to default the "/" slashes

You can use explode :

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

Output: 11111111111115

Documentation:

explode

substr

strlen

Predefined Constants

    
16.07.2018 / 20:30