2011-03-01 18 views
1

你可能已經看到的內容和提取鏈接和圖像的Facebook如何分析內容並提取鏈接的圖像和視頻分析PHP

當我們插入這樣的事情

this is an example text which is linked linke this http://stackoverflow.com/questions/ask 
and i like it so much like this picture  http://stackoverflow.com/img.png 

它變成這個

this is an example text which is linked linke this 
<a href='http://stackoverflow.com/questions/ask'> 
    and i like it so much like this picture 
<img src='http://stackoverflow.com/img.png'> 

現在我想要做的完全一樣,分析我的內容併產生一個豐富的內容。

我該如何在php中做這樣的事情?有沒有準備好的課程?

回答

0

您可以使用preg_replace以適當的正則表達式替換匹配的模式並添加HTML標記。

1

這些是兩個功能正常工作,並由我測試。

function isImage($url) { 
    if (substr($url, 0, 7) == 'http://') { 
     $i = str_ireplace('http://', '', $url); // delete first part 
     $i = explode('/', $i); // divide link into parts 
     $end = end($i); // get the last part 
     $ex = explode('.', $end); // get the extension if exists so for 'image.jpg' $ex = jpg 
     if (count($ex) === 2) { 
      $allowed = array('jpg', 'jpeg', 'png', 'gif'); // allowed image extensions 
      foreach ($allowed as $a) { 
       if ($ex = $a) { 
        # Is an image 
        return true; 
       } 
      } 
     } else { 
      # It's an url 
      return false; 
     } 
    } else { 
     # It's not even a link 
     return false; 
    } 
} 

function isLink($url) { 
    if (substr($url, 0, 7) == 'http://') { 
     $i = str_ireplace('http://', '', $url); // delete first part 
     $i = explode('/', $url); // divide link into parts 
     $end = end($i); // get the last part 
     $ex = explode('.', $end); // get the extension if exists so for 'image.jpg' $ex = jpg 
     return (count($ex) == 0) ? true : false; 
    } else { 
     # It's not even a link 
     return false; 
    } 
} 

$image = isImage($input); // true 
$link = isLink($input); // false 
var_dump($image, $link); 

實例(isLink()):

  • 'google.com'=假(*)
  • 'http://google.com'=真
  • 的「http:// google.com/image.png」 =假

實例(isImage()):

  • 'google.com'=假
  • 'google.com/image.png'=假(*)
  • 'http://google.com/image.png'=真
  • ' http://google.com/view/model/controller'= false

(*)=這是因爲它檢查鏈接是否有http://作爲前綴。如果你不希望這種情況發生,那麼這裏的功能:

function isImage($url) { 
    $i = explode('/', $i); // divide link into parts 
    $end = end($i); // get the last part 
    $ex = explode('.', $end); // get the extension if exists so for 'image.jpg' $ex = jpg 
    if (count($ex) === 2) { 
     $allowed = array('jpg', 'jpeg', 'png', 'gif'); // allowed image extensions 
     foreach ($allowed as $a) { 
      if ($ex = $a) { 
       # Is an image 
       return true; 
      } 
     } 
    } else { 
     # It's an url 
     return false; 
    } 
} 

function isLink($url) { 
    $i = explode('/', $url); // divide link into parts 
    $end = end($i); // get the last part 
    $ex = explode('.', $end); // get the extension if exists so for 'image.jpg' $ex = jpg 
    return (count($ex) == 0) ? true : false; 
} 

然後你可以使用它作爲:

if (isLink($url)) { echo "<a href=\"$url\">$url</a>"; } 
if (isImage($url)) { echo "<img src=\"$url\" alt=\"image\">"; } 
+0

感謝,但考慮IM輸入文本包含一些鏈接和圖片,那我該如何改變它? – 2011-03-01 12:08:18

+0

我更新了我的問題,也許這種呈現方式更好。 – 2011-03-01 12:12:01

+0

更新了我的答案(請參閱最後3行)。 – Shoe 2011-03-01 13:08:09