How to "unlock" any text at the time of "echo" and turn each word into a link

Asked

Viewed 494 times

4

I made a database and in this database register a phrase, I would like that when I was going to give the "echo" in that sentence, each word would become a unique link. Ex:

recue o código em 4 espaços

The phrase is simple, but I would like each word to create a link. The purpose of this is to improve the search media.

In the example phrase is the word code. In case I click on the word code I’d like to go to a search page that uses that word.

Using the word is simple, the biggest problem is unlocking that phrase and turning each word into a link.

  • A few hours crushing my mouse buttons to make a gun shoot properly an ET ended up in ICU. Re-added. ;)

3 answers

5


This is a tag system, usually keywords and not part of a text. They should be individual and not a phrase.

$str = 'recue o código em 4 espaços';
$str = explode( ' ' , $str );

foreach( $str as $palavra )
{
    if( mb_strlen( $palavra ) > 3 )
    echo '<a href="'.$palavra.'">'.$palavra.'</a>';
}

Note that each word will be a link. I added a check to accept words longer than 3 characters.

Example in ideone

  • Good +1. I thought it was in JS. Let’s see who got it right... After rereading the question I think your answer is the right one :P

  • all the answers were great and functional, but yours stands out for being simpler and have an easier application

  • This is prejudice against frighteningly hairy and complex codes. D

  • kkkkk @Brunoaugusto next time I get the most complex code, can it be? kkkk

  • It’s up to you. But if you get the hint, study my code. With it you learn a little about non interpretable loops (faster than a foreach, for example), Closures and frighteningly fantastic sprintf(). And learning about sprintf() automatically you are already able to use your raw printf(), sscanf(), fscanf() and vsprintf(). Péra... Using the cousins was kind of porn for this site... D

  • @Brunoaugusto, each with their own tools :) But the cool thing is that you can do the same thing in different ways. I wouldn’t be scared if your answer was the most voted.

Show 1 more comment

4

Javascript:

To break that sentence in the blanks you can do it this way:

var frase = 'recue o código em 4 espaços';
var palavras = frase.split(' ');
var links = palavras.map(function(palavra) {
    var link = document.createElement('a');
    var casulo = document.createElement('div');
    link.href = '?busca=' + palavra;
    link.innerHTML = palavra;
    casulo.appendChild(link);
    return casulo.innerHTML;
});
document.getElementById('resultado').innerHTML = links.join(' ');
<div id="resultado"></div>

This is a simplified way, could join more checks as for example delete short words like ou, a, é etc....

The variable links will have an array of anchors with word text.

  • can be on both sides. With JS it is common to see the highlighting of words in sponsored links and the ease of incorporating other features.

  • How the focus of the boy is SEO, search engines will recognize the links created by JS? [My question]

  • 1

    @Papacharlie nowadays know that Google already analyzes JS, but the right way to do this is right on the server side.

  • I just don’t understand the need for cloneNode since it is encapsulated...

  • @Kaduamaral nor I :P It must have been part of some idea I had and abandoned, forgetting to correct the code. I withdrew.

  • I think you were going to create the node once and reuse it in a loop or something. : P rsrs, dude +1

Show 1 more comment

2

And let the games begin! D

Another approach:

$str = 'recue o código em 4 espaços';

echo implode( ' ',

    array_map(

        function( $word ) {
            return sprintf( '<a href="%1$s">%1$s</a>', $word );
        },

        explode( ' ', $str )
    )
);

If you do not want the words to remain united by a space the way they were before the substitution, simply remove the implode().

Browser other questions tagged

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