cómo reemplazar una palabra con hipervínculo en una cadena

i am trying to build a function to change a word into a hyper link. i used this function

function myseo($t){
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$a=array('kid','children');
$uu=count($a);
for ($i = 1; $i <= $uu; $i++) {    
$theseotext= str_replace($a[$i], '<a href="'.$url[$i].'">'.$a[$i].'</a>', $t);  
}
return $theseotext;
}

but it doesnt work with me.

regards Piny

preguntado el 27 de agosto de 11 a las 17:08

2 Respuestas

I think it got scared, because it works with me now. ;)

function myseo($t){
    $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    $a = array('kid', 'children');
    $uu = count($a);
    $theseotext = $t;
    for ($i = 0; $i < $uu; $i++) {
        $theseotext = str_replace($a[$i], '<a href="'.$url.'">'.$a[$i].'</a>', $theseotext);
    }
    return $theseotext;
}

Demo

Respondido 27 ago 11, 21:08

@Pin Cody: I don't know why it got scared, but my theory is that it is a child. You see, it's replacing kids and children, it must be one to do so. :D - Shef

$theseotext= str_replace($a[i], '<a href="'.$url[i].'">'.$a[i].'</a>', $t);  

debiera ser

$theseotext= str_replace($a[$i], '<a href="'.$url.'">'.$a[$i].'</a>', $t);  

e intenta de nuevo

Respondido 27 ago 11, 21:08

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