I need help converting PHP eregi_replace to preg [duplicate]
-
30-04-2021 - |
Domanda
I know its a bit long. I'm trying to catch up on a deadline and if anyone could help me, I would gladly appreciate it. Another friend programmer of mine made this PHP script but unfortunately its already deprecated and doesn't work. Can anyone convert this to preg? I know nothing about eregi, preg, rejex, etc.
function tolink($text){
$text = " ".$text;
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'<a href="\\1" target="_blank" rel="nofollow">\\1</a>', $text);
$text = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'<a href="\\1" target="_blank" rel="nofollow">\\1</a>', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'\\1<a href="http://\\2" target="_blank" rel="nofollow">\\2</a>', $text);
$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4})',
'<a href="mailto:\\1" rel="nofollow">\\1</a>', $text);
return $text;
}
Soluzione
wouldn't it be like this
function tolink($text){
$text = " ".$text;
$text = preg_replace("/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/","<a href=\"$1\" target=\"_blank\" rel=\"nofollow\">$1</a>", $text);
$text = preg_replace("/(((f|ht){1}tps:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/","<a href=\"$1\" target=\"_blank\" rel=\"nofollow\">$1</a>", $text);
$text = preg_replace("/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/",
"$1<a href=\"http://$2\" target=\"_blank\" rel=\"nofollow\">$2</a>", $text);
$text = preg_replace('/([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4})/',
"<a href=\"mailto:$1\" rel=\"nofollow\">$1</a>", $text);
return $text;
}
you can also append i
at the end of preg_replace pattern to make so that lowercase and uppercase character will be matched
this is what i mean
preg_replace('/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i' .....
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow