Manipulating links with accents

Asked

Viewed 1,134 times

1

I am capturing news links posted on a particular site using the following code:

        function academia(){

            function makeNews(res){
                var soup = $(res.responseText).find('div [class~="tileImage"] h2');

                soup.each(function(e){

                    var title = $(this).find('a').text();
                    var link = $(this).find('a').attr('href');                              
                    var noticie = '<table class="noticias"><tr><td class="noticia_link" rowspan="3"><a href="http://www.fab.mil.br' + link + '" target="_blank">' + title + '</a></td></tr></table>';

                    $('#news-list').append(noticie);
                    $('.loading-alert').hide();
                    $('.news-list-title').css('visibility','visible');

                });
            }


            $.ajax({
                url: 'http://www.fab.mil.br/noticias/tag/SANTOS_DUMONT',
                type: 'GET',
                success: makeNews
            });
        }


        $(function() {
            academia();
        });

The problem is that the links have the headline of the news with accents and everything.

When I click on the page (generated with php) where they will be listed, the symbols appear switched. For example: http://www.fab.mil.br/noticias/mostra/18047/TREINAMENTO--Cadetes-da-Academia-daFor%E7a-A%E9rea-realizam-instru%E7%E3o-de-salto-de-emerg%Eancia-

They give 404 error when opened. I tried different Charsets in php but it does not solve.

Is there some simple output to capture the links exactly as they are?

ADDITION: This is the code that goes on the php page

    <div class="loading-alert">
    <h2>Buscando noticias...</h2>
    </div>
    <ul id="news-list"></ul>

And that’s the code that does the real work:

    /**
     * jQuery.ajax mid - CROSS DOMAIN AJAX 
     * ---
     * @author James Padolsey (http://james.padolsey.com)
     * @version 0.11
     * @updated 12-JAN-10
     * ---
     * Note: Read the README!
     * ---
     * @info http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
     */

     jQuery.ajax = (function(_ajax){

     var protocol = location.protocol,
     hostname = location.hostname,
     exRegex = RegExp(protocol + '//' + hostname),
     YQL = 'http' + (/^https/.test(protocol)?'s':'') + '://query.yahooapis.com/v1/public/yql?callback=?',
    query = 'select * from html where url="{URL}" and xpath="*"';

function isExternal(url) {
    return !exRegex.test(url) && /:\/\//.test(url);
}

return function(o) {

    var url = o.url;

    if ( /get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url) ) {

        // Manipulate options so that JSONP-x request is made to YQL

        o.url = YQL;
        o.dataType = 'json';

        o.data = {
            q: query.replace(
                '{URL}',
                url + (o.data ?
                    (/\?/.test(url) ? '&' : '?') + jQuery.param(o.data)
                : '')
            ),
            format: 'xml'
        };

        // Since it's a JSONP request
        // complete === success
        if (!o.success && o.complete) {
            o.success = o.complete;
            delete o.complete;
        }

        o.success = (function(_success){
            return function(data) {

                if (_success) {
                    // Fake XHR callback.
                    _success.call(this, {
                        responseText: (data.results[0] || '')
                            // YQL screws with <script>s
                            // Get rid of them
                            .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
                    }, 'success');
                }

            };
        })(o.success);

    }

    return _ajax.apply(this, arguments);

     };

     })(jQuery.ajax);
  • Using Google Chrome 36 shows up all right. Can share print screens to better understand your problem?

  • You say you opened this link in Chrome and loaded the page correctly? For me it appears with the following error, both in Chrome and IE: "Oops! It appears that your request generated an error 500. If the error persists, please contact an Administrator. " The right link would be: http://www.fab.mil.br/noticias/mostra/18047/TREINAMENTO---Cadetes-da-Academia-For%C3%A7aa-A%C3%A9rea-realizam-instru%C3%A7%C3%A3o-de-salto-de-emerg%C3%Aancia-

  • 1

    If you simply remove the Slug link works correctly: http://www.fab.mil.br/noticias/mostra/18047/

  • I saw it, but I can’t think of how to do it. Some direction you can point me to in javascript or php?

  • you receive the changed accents or when you generate the links they exchange?

  • They only change on the generated page. This is her code: <?php include('header.php'); ?>&#xA;&#xA;<script src="assets/js/news-g1.js"></script>&#xA;&#xA;&#xA;<div id="conteudo">&#xA;&#xA;<div class="lista">&#xA;<table class="noticias"><tr class="g1"><td class="noticia_link" rowspan="3"> </td></tr></table>&#xA;<div class="loading-alert"><h2>Buscando noticias...</h2></div>&#xA; <ul id="news-list"></ul>&#xA;&#xA;</div>&#xA;&#xA;</div>&#xA; &#xA; &#xA;<?php include('footer.php'); ?>

  • friend, this code block does absolutely nothing relative to the problem - it is pure HTML... if you receive the correct accents (áéíóú) the problem can be the encoding, is using UTF-8?

Show 2 more comments

1 answer

0


Can remove the Slug by regex:

<?php
$url = "http://www.fab.mil.br/noticias/mostra/18047/TREINAMENTO---Cadetes-da-Academia-da-For%E7a-A%E9rea-realizam-instru%E7%E3o-de-salto-de-emerg%EAncia-";
preg_match('~(.+)(\d*\/)~', $url, $semSlug);
var_dump($semSlug[1]);
?>
  • I don’t know how to do this by automatically putting the link

  • 4

    @Roberto, the Stack Overflow in Portuguese is not a service make my code. My answer takes you to the right path and doesn’t take much to adapt what you already have. Try to understand the Script you posted from James Padolsey and adapt it along with what I posted for your needs or hire a programmer to do the job.

Browser other questions tagged

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