PHP でpreg_replace関数を使用しており、ユーザーが送信した URL を bit.ly 短縮リンクに置き換えようとしています。
$comment = preg_replace( '/(http|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '', $strText );
これにより、コメントのみが表示され、URL が「一掃」されます。問題は、テキストから URL を取得して後で追加するにはどうすればよいかということです。
PHP でpreg_replace関数を使用しており、ユーザーが送信した URL を bit.ly 短縮リンクに置き換えようとしています。
$comment = preg_replace( '/(http|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '', $strText );
これにより、コメントのみが表示され、URL が「一掃」されます。問題は、テキストから URL を取得して後で追加するにはどうすればよいかということです。
php.net の例:
<?php
// this text was used in 2002
// we want to get this up to date for 2003
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// the callback function
function next_year($matches)
{
// as usual: $matches[0] is the complete match
// $matches[1] the match for the first subpattern
// enclosed in '(...)' and so on
return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
"|(\d{2}/\d{2}/)(\d{4})|",
"next_year",
$text);
?>
URL を置き換えるコールバック関数を定義します。一致をパラメーターとして受け取り、それらの一致に応じて、置換文字列を内部で形成します。