First you have to read the XML format, then you can use DOMDocument
or SimpleXML
, for your case, it seems simple the second resolves:
For example:
<?php
$xmlstring = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.site.com.br/aluno/jose/11111111111111/</loc>
</url>
<url>
<loc>https://www.site.com.br/aluno/jose/22222222222222/</loc>
</url>
<url>
<loc>https://www.site.com.br/aluno/jose/33333333333333/</loc>
</url>
</urlset>';
$xml = simplexml_load_string($xmlstring);
foreach ($xml as $tag) {
var_dump($tag->loc);
}
I made the example assuming it is a sitemap
Now if XML is something like:
<foo>
<loc>https://www.site.com.br/aluno/jose/11111111111111/</loc>
<loc>https://www.site.com.br/aluno/jose/22222222222222/</loc>
<loc>https://www.site.com.br/aluno/jose/33333333333333/</loc>
</foo>
That would be enough:
<?php
$xmlstring = '<?xml version="1.0" encoding="UTF-8"?>
<foo>
<loc>https://www.site.com.br/aluno/jose/11111111111111/</loc>
<loc>https://www.site.com.br/aluno/jose/22222222222222/</loc>
<loc>https://www.site.com.br/aluno/jose/33333333333333/</loc>
</foo>';
$xml = simplexml_load_string($xmlstring);
foreach ($xml as $tagFoo) {
var_dump($tagFoo);
}
If it comes from a URL you can use $xml = simpleXML_load_file('http://site/sitemap.xml');
instead of $xml = simplexml_load_string($xmlstring);
Whatever format it is, just adjust to read, now we go to the point to get the last part of the string.
I could do a function like one of these two:
With substr
function final_str($str, $delimitador = '/') {
$str = trim($str, $delimitador); //Remove o delimitador do final
$posicao = strrpos($str, $delimitador) + 1; //Pega a posição do ultimo delimitador (no seu caso /)
return substr($str, $posicao); // Remove tudo antes do delimitador incluindo o delimitador
}
With explode
function final_str($str, $delimitador = '/') {
$str = trim($str, $delimitador); //Remove o delimitador do final
$partes = explode($delimitador, $str); //Separa a string em partes
return $partes[count($partes) - 1]; //Pega a parte final da string
}
And wear it like this:
$xml = simplexml_load_string($xmlstring);
foreach ($xml as $tag) {
var_dump( final_str($tag->loc) );
}
Or so if it comes from a URL:
$xml = simplexml_load_string('http://site.com/sitemap.xml');
foreach ($xml as $tag) {
var_dump( final_str($tag->loc) );
}
I couldn’t. What would it look like in the code? Remembering that I need to get all the last numbers from all the sitemap urls. I changed the function and the return is: url>
– Marcelo
@Marcelo has how you put the
Xml
in your question, what are you really using?– novic
It worked like you did! Thank you! I only have one problem: is it possible to put the sitemap url instead of the content? in $xml =
– Marcelo
yes of course just do so @Marcelo put a comment to use
simpleXML_load_file($url)
– novic