Hari ini saya sedang coding dan mencari snippet untuk merubah link di text dengan menyisipkan nofollow di dalamnya dan saya mendapatkan script ini
1 2 3 4 5 6 7 8 9 10 11 |
function nofollow($html, $skip = null) { return preg_replace_callback( "#(<a[^>]+?)>#is", function ($mach) use ($skip) { return ( !($skip && strpos($mach[1], $skip) !== false) && strpos($mach[1], 'rel=') === false ) ? $mach[1] . ' rel="nofollow">' : $mach[0]; }, $html ); } |
Cara Penggunaan
1 2 3 4 5 6 7 8 |
echo nofollow('<a href="link somewhere" rel="something">something</a>'); // will be same because it's already contains rel parameter echo nofollow('<a href="http://www.cnn.com">something</a>'); // ad // add rel="nofollow" parameter to anchor echo nofollow('<a href="http://localhost">something</a>', 'localhost'); // skip this link as internall link |
Script sudah saya test dan berhasil bekerja dengan baik.
Selamat mencoba
*sumber http://stackoverflow.com/questions/5037592/how-to-add-rel-nofollow-to-links-with-preg-replace