function url_to_external_blog_card($the_content) { if (is_singular()) { //投稿ページもしくは固定ページのとき //1行にURLのみが期待されている行(URL)を全て$mに取得 $res = preg_match_all('/^(<p>)?(<a.+?>)?https?:\\/\\/[-_.!~*\'()a-zA-Z0-9;\\/?:\\@&=+\\$,%#]+(<\\/a>)?(<\\/p>)?(<br ? \\/>)?$/im', $the_content, $m); //マッチしたURL一つ一つをループしてカードを作成 foreach ($m[0] as $match) { $url = strip_tags($match); //URL $tag = url_to_external_blog_card_tag($url); if (!$tag) { continue; } // //サイトの内部リンクは処理しない場合 // if ( strpos( $url, get_this_site_domain() ) ) { // continue ; // } // //取得した情報からブログカードのHTMLタグを作成 // $tag = '<'.'iframe '.'class="blog-card external-blog-card" src="//hatenablog.com/embed?url='.$url.'"></'.'iframe'.'>'; //本文中のURLをブログカードタグで置換 $the_content = preg_replace('{' . preg_quote($match) . '}', $tag, $the_content, 1); } } return $the_content; //置換後のコンテンツを返す }
function url_to_external_blog_card($the_content) { if (is_singular()) { //投稿ページもしくは固定ページのとき //1行にURLのみが期待されている行(URL)を全て$mに取得 $res = preg_match_all('/^(<p>)?(<a.+?>)?https?:\\/\\/[-_.!~*\'()a-zA-Z0-9;\\/?:\\@&=+\\$,%#]+(<\\/a>)?(<\\/p>)?(<br ? \\/>)?$/im', $the_content, $m); //マッチしたURL一つ一つをループしてカードを作成 foreach ($m[0] as $match) { $url = strip_tags($match); //URL $tag = url_to_external_blog_card_tag($url); if (!$tag) { continue; } //本文中のURLをブログカードタグで置換 $the_content = preg_replace('{' . preg_quote($match) . '}', $tag, $the_content, 1); } } return $the_content; //置換後のコンテンツを返す }