thebarless
thebarless

Reputation: 472

replacing dynamic text with an image

I am trying to create a simplified code to insert images dynamically into a page based on user entry similar to BBCode.

For example, if one of my users types "I like ducks [image]ducks[/image]", I want to explode the [image]ducks[/image], search MySQL for the keyword "ducks", pull the image path & name from the database that matches, then display the image HTML code as well as the source to the image.

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);

The wall I'm hitting is how to replace the text inside a dynamically generated page if it exists - and leave the content alone if the code doesn't exist. Any ideas?


EDIT - Complicating the problem:

Thanks for helping! I used your input to make the following function:

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;
  }

I need this to work regardless of how many instances it occurs (thus if my user writes [image]duck[/image] then two sentences later writes [image]cow[/image], I want the function to replace both with their respective result). As it stands now, with more than one instance, it errors (not a valid SQL resource) which makes sense since preg_match only looks for one. I tried creating a loop (while & foreach w/ preg_match_all) to try testing the concept - both created infinite loops and my web server admin isn't too happy :p

Upvotes: 1

Views: 1717

Answers (1)

Aaron Hathaway
Aaron Hathaway

Reputation: 4315

I would try doing it with preg_match to get the image_url and preg_replace to replace it:

$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;

edit

$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;

Upvotes: 1

Related Questions