2012-10-12 9 views
0

사람들은 온라인으로 이동하여 제목과 설명과 함께 기사를 제출합니다. 사람들은 종종 링크를 포함하지만 기본 텍스트로 나타납니다. 사람들이 URL을 포함하면 작동 링크로 인식되는 방식을 원합니다. 코드를 작성했지만 한 행만 스캔하고 실제 테이블에서 작동하지 않는 것처럼 보입니다. 실제 테이블에서는 테이블 외부에서 반향됩니다.mysql 행의 URL 추출

기본적으로 ... 링크를 제출하면 하이퍼 링크가 만들어지는 테이블이 필요합니다. 어떤 아이디어? 아래의 업데이트 된 코드는 동일한 일을 계속합니다.

내 코드는 다음과 같습니다 : 당신이 preg_replace로 교체하기 전에 당신이 인쇄 때문에

 $query = "SELECT * FROM rumours ORDER BY id DESC"; 
    $query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error($connect)); 
    while ($row = mysql_fetch_assoc($query)) { 
$id = $row['id']; 
$band = $row['band']; 
$title = $row['Title']; 
$description = $row['description']; 

$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; 

// The Text you want to filter for urls 
// Check if there is a url in the text 
    if(preg_match($reg_exUrl, $description, $url)) { 
    // make the urls hyper links 
    preg_replace($reg_exUrl, '<a href="'.$url[0].'">'.$url[0].'</a>', $description); 

     } 

    echo "<table border='1'>"; 
    echo "<tr>"; 
    echo "<td> $title </td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td class = 'td1'> $description </td>"; 
    echo "</tr>"; 
    echo "</table>"; 

} 
+0

이 질문보기 : http://stackoverflow.com/questions/1188129/replace-urls-in-text-with-html-links – Ollie

답변

0

그건 ...

그렇게 루프 내부에 레그를 넣어 :

$query = "SELECT * FROM rumours ORDER BY id DESC"; 
$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error($connect)); 
while ($row = mysql_fetch_assoc($query)) { 
    $id = $row['id']; 
    $band = $row['band']; 
    $title = $row['Title']; 
    $description = $row['description']; 

    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; 

    // The Text you want to filter for urls 

    // Check if there is a url in the text 
    if(preg_match($reg_exUrl, $description, $url)) { 
      // make the urls hyper links 
      preg_replace($reg_exUrl, '<a href="'.$url[0].'">'.$url[0].'</a>', $description); 

    } 

    echo "<table border='1'>"; 
    echo "<tr>"; 
    echo "<td> $title </td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td class = 'td1'> $description </td>"; 
    echo "</tr>"; 
    echo "</table>"; 

} 

그건 그렇고, 일반적인 mysql_ 함수 대신에 PDO 나 mysqli_를 사용해보십시오.

+0

코드가 변경되지 않았습니다. 모두 –

0

$reg_exUrl 아무 데나 에코되지 않습니다. 따라서 a href이 표시되지 않습니다

+0

어디서 반향합니까? –