DMMのアフィリエイトID書き換え

PHPの正規表現とリプレイスでやればこんな感じかな?

function replace_link($str) {
 $pattern = '/(<a href=")[^>]+(>[^<]+<\/a>)/';
 preg_match_all($pattern,$str,$matches) ;

 for($i = 0 ;$i < count($matches[0]) ;$i++ ) {
  preg_match_all($pattern,$matches[0][$i],$matches_link) ;
  $link = str_replace($matches_link[1][0],"",$matches[0][$i]);
  $link = str_replace($matches_link[2][0],"",$link);
  $link_array = explode('/',$link);
  $new_link = "";
  for($h=0;$h     $new_link .= $link_array[$h].'/';
  }
  $new_link .= '自分のアフィリエイトID" target="_blank"';
  $replace_link = "$1$new_link$2";
  $matches_r[] = preg_replace($pattern,$replace_link,$matches[0][$i]) ;
 }
 return str_replace($matches[0],$matches_r,$str) ;
}

Comment Form

コメントを表示する前に、管理人の承認が必要になることがあります。その場合は、承認されるまでコメントは表示されませんので、ご了承ください。

スタイル用のHTMLタグが使えます

Trackback

Recent entry

Search

Tag Cloud

Page Top