2011-02-08 5 views
1

BBCode와 비슷한 사용자 입력을 기반으로 이미지를 페이지에 동적으로 삽입하는 간단한 코드를 만들려고합니다.동적 텍스트를 이미지로 대체

예를 들어, 사용자 중 하나가 "오리 [이미지] 오리 [/ 이미지]"를 입력하면 [이미지] 오리 [/ 이미지]를 폭발시키고 MySQL에서 키워드 "오리"를 검색하고 싶습니다. 일치하는 데이터베이스에서 이미지 경로 &의 이름을 가져온 다음 이미지의 HTML 코드와 소스를 이미지에 표시하십시오. 그리고 코드가 존재하지 않는 경우 혼자 항목을 그대로 유지 -

function image_replace($dimg){ 
    list($title) = explode("[image]",$dimg); 
    $query_image = mysql_query("SELECT * FROM images WHERE image_title LIKE '%$title%'"); 
    $fetch_image = mysql_fetch_array($query_image); 
    $image_path = $fetch_image['image_path']; 
    $image_filename = $fetch_image['image_filename']; 
    $image_source = $image_path.$image_filename; 
    $dimg = str_replace("[image]","<img src=\"$image_source\">", $dimg); 
    $dimg = str_replace("[/image]","</img>", $dimg); 
    $dimg = str_replace("$title", "", $dimg); 
    return $img; 
    } 

image_replace($ducks); 

내가 타격있어 벽이 존재하는 경우 동적으로 생성 된 페이지 내의 텍스트를 교체하는 방법입니다. 어떤 아이디어?


편집 - 문제를 복잡하게 : 돕는

감사합니다! 나는 다음과 같은 기능을 확인하기 위해 입력을 사용 :이 그것이 발생 인스턴스 수에 관계없이 작업해야

function image_replace($string){ 
    $matches = array(); 
    preg_match('/\[image\](.*)\[\/image\]/', $string, $matches); 
    $image = $matches[1]; 
    $query_image = mysql_query("SELECT * FROM images WHERE image_title LIKE '%$image%'"); 
    $fetch_image = mysql_fetch_array($query_image); 
    $image_path = $fetch_image['image_path']; 
    $image_filename = $fetch_image['image_filename']; 
    $image_source = $image_path.$image_filename; 
    $image_url = "<img src=\"$image_source\"></img>"; 
    $new_string = preg_replace('/\[image\](.*)\[\/image\]/', $image_url, $string); 

    return $new_string; 
    } 

을 (따라서 내 사용자가 쓰는 경우 [영상] 오리 [/ 이미지] 다음 두 문장 나중에 [이미지를 기록 ] 젖소 [/ 이미지], 나는 그들의 각각의 결과로 둘 다 바꿀 기능을 원한다). 이제는 하나 이상의 인스턴스에서 preg_match가 하나만 찾는 오류 (유효하지 않은 SQL 리소스가 아닙니다)가 있습니다. P

+0

새로운 문제를 고려. 나는 그것과 약간 꼼작 할 것이다. –

+0

알았어 내 남자, 내 대답에 대한 편집을 한번보고 그 트릭을한다면 알려주세요! –

답변

1

나는 이미지 URL을 얻을 preg_match으로 그 일을 시도 할 것입니다 : 모두 만든 무한 루프 내 웹 서버 관리자가 너무 행복하지 않다 - 나는 (으로, preg_match_all/w &의 foreach는 동안) 테스트 개념을 시도하는 루프를 만드는 시도 그리고 preg_replace 그것을 대체 :

$string = 'I like ducks [image]ducks[/image]'; 
echo 'Before: ' . $string . '<br />'; 
$matches = array(); 
preg_match('/\[image\](.*)\[\/image\]/', $string, $matches); 
$image = $matches[1]; 
//Lookup image_url and throw it in an <img> 
$image_url = 'http://blah.com'; //Don't forget <img> 
$new_string = preg_replace('/\[image\](.*)\[\/image\]/', $image_url, $string); 
echo 'After: ' . $new_string; 

편집

$string = "<br />I like ducks [image]ducks[/image]<br />I like cows [image]cows[/image]<br />I like pigs [image]pigs[/image]"; 
echo 'Before: ' . $string . '<br />'; 

$matches = array(); 
preg_match_all('/\[image\]([^\[]*)\[\/image\]/', $string, $matches); 
$image_names = $matches[1]; 
foreach($image_names as $image_name) { 
    //Perform your lookup on each name 
    //If it is valid perform the replace 
    //Gonna say cows isn't there to test 
    if($image_name != 'cows') { 
     $image_url = 'http://blah.com'; //result of lookup 
     $string = preg_replace('/\[image\]' . $image_name . '\[\/image\]/', $image_url, $string); 
    } 
} 
echo 'After: ' . $string; 
+0

위대한 작품! 도움을 주셔서 감사합니다. 심지어 preg_match도 고려하지 않았습니다. – thebarless

+0

Perl 정규식이 최선의 방법입니다. – MeanEYE

+0

'. *'대신'[^ \ [] *'을 사용하고'preg_match_all'을 사용하면 그렇지 않습니다. 한 이미지 만 교체하면됩니다. –

관련 문제