¿Cómo seleccionar múltiples textos con @ como la primera letra del texto dentro de una línea de textos?

Okay, the header question may sound confusing, yeah, I too was confused. Anyway, what I wanted is this: Say I have this line of text,

The quick brown @fox jumps @over the @lazy dog.

This line of text is a dynamically fetched 'single line' from the database, not an array of texts. Assuming the texts with first letter as '@' are links to a page or something, I want that I can specify on where to put my anchor tag, in my case, I want to put anchor tag on each of the texts starting with '@'.

I tried explode, but it seems explode is not the answer to this. Can someone help me out here? Thanks.

preguntado el 02 de julio de 12 a las 19:07

Once you've processed this line with PHP what do you want it to look like, what end-result are you looking for? -

@mario, thank you for letting me know but my problem's a bit different. but thanks anyway. -

2 Respuestas

No quieres usar explode for that, but a regex. To match multiple occurences, preg_match_all is the deal.

preg_match_all('/@\w+/', $input, $matches);

        #        @   is the literal "@" character
        #    and \w+ matches consecutive letters

You certainy might want to use preg_replace to transform them into links. Or better yet preg_replace_callback to move some logic into a handler function.

Respondido 02 Jul 12, 19:07

Agree 100% on the regex approach. Beat me to it! - mike macintosh

Wow, this is new to me. But, I'm in a sh***y situation right now, I somehow understand the flow of your answer, but it seems I find it hard to implement. I searched a while ago for preg_match_all at php.net/manual, but can you please elaborate on what should I supposedly do? *feelsdumbrightnow - Tsukimoto Mitsumasa

aw yeah, eventually I got it. Thanks guys. Now I'll be troubling myself with preg_replace now. - Tsukimoto Mitsumasa

@CHiRiLo: The linked duplicate contained a similar method. See also stackoverflow.com/questions/4766158/… - Mario

You can use explode to process the words that have a @ before... It really depends on what you want to do:

//Store the string in a variable
$textVar = "The quick brown @fox jumps @over the @lazy dog.";

//Use explode to separate words
$words = explode(" ", $textVar);

//Check all the variables in the array, if the first character is a @
//keep it, else, unset it
foreach($words as $key=>$val) {
    if(substr($val, 0, 1) != "@") {
        unset($words[$key]);
    } else {
        $words[$key] = "<a href='#'>".$words[$key]."</a>";
    }
}

//You can now printout the array and you will get only the words that start with @
foreach($words as $word) {
    echo $word."<br>";
}

You could also keep the strings that dont have a @ and use an implode to put everything together:

//Store the string in a variable
$textVar = "The quick brown @fox jumps @over the @lazy dog.";

//Use explode to separate words
$words = explode(" ", $textVar);

//Check all the variables in the array, if the first character is a @
//keep it, else, unset it
foreach($words as $key=>$val) {
    if(substr($val, 0, 1) != "@") {
        //Do nothing
    } else {
        $words[$key] = "<a href='#'>".$words[$key]."</a>";
    }
}

//You can now printout the string
$words = implode($words, " ");
echo $words;

Respondido 02 Jul 12, 20:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.