Note that there are some explanatory texts on larger screens.

plurals
  1. POreplacing dynamic text with an image
    primarykey
    data
    text
    <p>I am trying to create a simplified code to insert images dynamically into a page based on user entry similar to BBCode.</p> <p>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 &amp; name from the database that matches, then display the image HTML code as well as the source to the image.</p> <pre><code>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]","&lt;img src=\"$image_source\"&gt;", $dimg); $dimg = str_replace("[/image]","&lt;/img&gt;", $dimg); $dimg = str_replace("$title", "", $dimg); return $img; } image_replace($ducks); </code></pre> <p>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?</p> <hr> <p><strong>EDIT - Complicating the problem:</strong></p> <p>Thanks for helping! I used your input to make the following function:</p> <pre><code>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 = "&lt;img src=\"$image_source\"&gt;&lt;/img&gt;"; $new_string = preg_replace('/\[image\](.*)\[\/image\]/', $image_url, $string); return $new_string; } </code></pre> <p>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 &amp; foreach w/ preg_match_all) to try testing the concept - both created infinite loops and my web server admin isn't too happy :p</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload