Below is a script I've been working on with the help of people and threads in these forums.
The idea is to strip any occurrences of "[image]any-number-here[/image]" (from the $string) of its tags, and leave you with just the number. When you've got that number, a query is then made against the appropriate table in the database in order to get the details of an image (thumbnail path etc), and then that information needs to be converted into proper HTML <img> tags and inserted at the exact point in the $string where the original "[image]" commands where.
Here's the script (I am still a newbie, so go soft please!)...
<?
$string = 'blah blah .. [image]1[/image] ..';
$pattern1 = '[image][0-9]{1,}';
$pattern2 = '[\/image]';
$search_for_id = ereg($pattern1, $string, $arr_id);
// arr_id[0] will carry the matched string i.e. [image]001
// this will chop off the first 7 characters i.e. [image]
while ($imageid = substr($arr_id[0], 7))
{
mysql_connect (localhost, mylogin, mypass) OR DIE("Unable to connect to database");
mysql_select_db (mydb) OR DIE("Unable to connect to database");
$imageresult = mysql_query ("select ThumbPath FROM Photos where PhotoID=$imageid");
if ($row = mysql_fetch_array($imageresult))
do {
print $row["ThumbPath"];
}
};
?>
...the first problem is a Parser Error. The error message states: expecting 'WHILE' on Line 30 (which is the last '};' in the script). I cannot get my head around this or where I am going wrong. It must be really obvious. Incidentally, on the line above the Parser Error message, I can see a correct image path retrieved from the database, matching to record 1 - so the basics of the script must be working.
...the second problem is something I can forsee going wrong once the above error is fixed. $string is eventually going to be swapped in place of a query result from another table of my database, which will carry the 'longblob' of text that contains these special [image] blocks. When I test this script and get the parser error, as I said above, I do see the correct image path name. However I do not see the "blah blah" text from the $string that is sitting around the [image] blocks. I need to leave whatever is in $string in its place and appear on the finished page, but have the [image] blocks strip down and convert to the necessary HTML <img> tags using path information from the database.
Can anybody help me finish this one off? I'm really struggling here. Thanks.
Mark