Ho una funzione che aggiungerà il tag <a href>
prima di un collegamento e </a>
dopo il collegamento. Tuttavia, si interrompe per alcune pagine Web. Come miglioreresti questa funzione? Grazie!PHP - Aggiungi il link a un URL in una stringa
function processString($s)
{
// check if there is a link
if(preg_match("/http:\/\//",$s))
{
print preg_match("/http:\/\//",$s);
$startUrl = stripos($s,"http://");
// if the link is in between text
if(stripos($s," ",$startUrl)){
$endUrl = stripos($s," ",$startUrl);
}
// if link is at the end of string
else {$endUrl = strlen($s);}
$beforeUrl = substr($s,0,$startUrl);
$url = substr($s,$startUrl,$endUrl-$startUrl);
$afterUrl = substr($s,$endUrl);
$newString = $beforeUrl."<a href=\"$url\">".$url."</a>".$afterUrl;
return $newString;
}
return $s;
}
La regex è un po 'sciatto, ma il 99% del mio ingresso avrà URL corretti eventuali – AlexBrand
Cosa pagine web non è per rompere? –
All'inizio si prova anche https agains, ma in seguito si omette la "s". Non so, se questo causa questo errore, perché anche io non so, quali pagine sono rotte;) – KingCrunch