Michan Posted November 7, 2007 Share Posted November 7, 2007 Hi, I'm having some problems trying to do something; I just can't think of a way to do it. Basically, I want to search for a specific snippet of text in a body of text, then replace it with the requested database result with some thrown in HTML. Users will be able to type the following (or something along these lines, depending on what works best): [image=12345]Insert caption here[/image] Then I need it to search for the requested image in the appropriate table and return the image's url (for an example, let's say the table is called "images", and the columns required are "imageid" and "imageurl". And finally, it needs to be replaced in the following format: <img src="link.to/requested/image.jpg"><BR>Insert caption here I've seen it done before, so there must be a way. I just need a little help understanding that method. Thanks in advance for your time. - Mi Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 7, 2007 Share Posted November 7, 2007 This should help you: http://phpsnips.com/snippet.php?id=41 If you have any questions, ask Ill help Quote Link to comment Share on other sites More sharing options...
Michan Posted November 7, 2007 Author Share Posted November 7, 2007 This should help you: http://phpsnips.com/snippet.php?id=41 If you have any questions, ask Ill help Thanks, I'm aware that this exists, but I just can't figure out a way to select a result from the database and return it. Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 7, 2007 Share Posted November 7, 2007 Assuming the format: [image=12345]Insert caption here[/image] Then use a preg_match combined with a database search $pattern = "/\[image=([^\]]*)\]/"; preg_match($pattern,$string,$matches); $image_id = matches[1];//this will be the id you can query in the database for the correct url //do the query here, yadyada $row = mysql_fetch_array($query); $pattern_find = "/\[image=([^\]]*)\]([^\[]*)\[\/image\]/"; $pattern_replace = "<img src=".$row['url']."><BR>$2"; preg_replace($pattern_find,$pattern_replace,$string); That will do it (I may have a slight typo, but as far as my knowledge goes with this stuff, it should work). $2 will be whatever is matched inside the second set of parentheses ([^\[]*), also known as the backreference (you can google this if you want more information on how it works). Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 7, 2007 Share Posted November 7, 2007 <?php $orig = '[image=12345]Insert caption here[/image]'; $find = '~[image=(.*)](.*)[/image]~e'; if(preg_match($find,$orig,$matches)){ $num = preg_replace("~image=(.*)\](.*)~","$1",$matches[0]); $cap = preg_replace("~image=(.*)\](.*)\[\/image~","$2",$matches[0]); echo $num; echo $cap; $sql = mysql_query("SELECT * FROM images WHERE imageid=$num"); $row = mysql_fetch_array($sql); echo '<img src="'.$row['imageurl'].'"><br>'.$cap; } ?> Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 7, 2007 Share Posted November 7, 2007 <?php $orig = '[image=12345]Insert caption here[/image]'; $find = '~[image=(.*)](.*)[/image]~e'; if(preg_match($find,$orig,$matches)){ $num = preg_replace("~image=(.*)\](.*)~","$1",$matches[0]); $cap = preg_replace("~image=(.*)\](.*)\[\/image~","$2",$matches[0]); echo $num; echo $cap; $sql = mysql_query("SELECT * FROM images WHERE imageid=$num"); $row = mysql_fetch_array($sql); echo '<img src="'.$row['imageurl'].'"><br>'.$cap; } ?> Just to point out, this wouldn't work as you have to escape [ and ] (since those define range of values like [a-zA-Z0-9] if you want them to be treated literally). Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 7, 2007 Share Posted November 7, 2007 It does so work.... I tested it on my server. Quote Link to comment Share on other sites More sharing options...
Michan Posted November 7, 2007 Author Share Posted November 7, 2007 I've been spending some time implementing both of these methods, substituting some code, and even integrating them somewhat. I haven't had any success yet, though. Neither of the methods seem to work for me, unfortunately. Unless I'm doing something terribly wrong. Please help! Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 7, 2007 Share Posted November 7, 2007 want to post what you have? Quote Link to comment Share on other sites More sharing options...
Michan Posted November 7, 2007 Author Share Posted November 7, 2007 want to post what you have? I've been through many variations, but this is what I have at the moment: $pattern = "~[image=(.*)]~e"; preg_match($pattern,$showarticle['artcontent'],$matches); $image_id = $matches[1]; if($image_id) { $getimage = mysql_query('SELECT * FROM vg_files WHERE id ='.$image_id); $image = mysql_fetch_array($getimage); $pattern_find = "~[image=(.*)](.*)[/image]~e"; $pattern_replace = "<img src=".$image['file']."><BR>$2"; preg_replace($pattern_find,$pattern_replace,$showarticle['artcontent']); } Quote Link to comment Share on other sites More sharing options...
Michan Posted November 7, 2007 Author Share Posted November 7, 2007 Could somebody help with a solution here, please? Quote Link to comment Share on other sites More sharing options...
Michan Posted November 7, 2007 Author Share Posted November 7, 2007 Okay, I have a semi-working piece of code. The only problem is that it only collects the first image called. How would I get about selecting the second, third, and more? $string = '[image=1234]This is a caption[/image]<BR><BR>[image=999]Caption LUL![/image]'; $pattern = "#\[image=(.*?)\]#si"; preg_match($pattern,$string,$matches); $image_id = $matches[1]; $sql = mysql_query("SELECT file, album FROM vg_files WHERE id=$image_id"); $row = mysql_fetch_array($sql); $albumidstrip = explode('|', $row['album']); $albumid = ($albumidstrip[1]); $sql2 = mysql_query('SELECT directory FROM vg_albums WHERE id = '.$albumid); $row2 = mysql_fetch_array($sql2); $pattern_find = "#\[image=(.*?)\](.*?)\[\/image\]#si"; $pattern_replace = '<img src="downloads/'.$row2['directory'].'/'.$row['file'].'"><BR>$2'; $string = preg_replace($pattern_find,$pattern_replace,$string); echo $string; Quote Link to comment Share on other sites More sharing options...
Michan Posted November 7, 2007 Author Share Posted November 7, 2007 Anybody? Please? I can provide a link if required. Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 8, 2007 Share Posted November 8, 2007 For multiple matches, you will have to use the preg_match_all() function [rather than the preg_match() which stops after the first result] Quote Link to comment Share on other sites More sharing options...
Michan Posted November 8, 2007 Author Share Posted November 8, 2007 Thank you all so much for your help! We're almost there; I can tell by changing the variable of the $matches[$i] to 1, 2, etc - as it delivers a different image for each. Right now though, it's still displaying the first image above every different caption. Could somebody please tell me what little bit of code I need to add/remove/change to fix this, and get every image to show rather than just the first one? $text = '[image=1234]Caption 1![/image]Blah blah[image=4321]Caption 2.[/image]'; $pattern = "#\[image=(.*?)\]#si"; preg_match_all($pattern,$text,$matches, PREG_SET_ORDER); for ($i = 0; $i < count($matches); $i++) { $sql = mysql_query("SELECT file FROM vg_files WHERE id=".$matches[$i][1]); $row = mysql_fetch_array($sql); $pattern_find = "#\[image=(.*?)\](.*?)\[\/image\]#si"; $pattern_replace = ''.$row['file']."<BR>$2"; $text = preg_replace($pattern_find,$pattern_replace,$text); } Outputs as: <img src="link.to/1234.jpg"><BR>Caption 1!Blah blah<img src="link.to/1234.jpg"><BR>Caption 2. Many thanks for your help! - Mi Quote Link to comment Share on other sites More sharing options...
Michan Posted November 8, 2007 Author Share Posted November 8, 2007 Anybody? It must be something so simple, please help me out here! Quote Link to comment Share on other sites More sharing options...
Michan Posted November 8, 2007 Author Share Posted November 8, 2007 Okay, the problem I seem to be having is that the $i isn't being updated to 1, 2, 3 and soforth. $text = '[image=1234]Caption 1![/image]Blah blah[image=4321]Caption 2.[/image]'; $pattern = "#\[image=(.*?)\]#si"; preg_match_all($pattern,$text,$matches, PREG_SET_ORDER); for ($i = 0; $i < sizeof($matches); $i++) { $sql = mysql_query("SELECT file FROM vg_files WHERE id=".$matches[$i][1]); $row = mysql_fetch_array($sql); $pattern_find = "#\[image=(.*?)\](.*?)\[\/image\]#si"; $pattern_replace = ''.$row['file']."<BR>$2"; $text = preg_replace($pattern_find,$pattern_replace,$text); } I've been experimenting for hours now, with no success. If somebody could please guide me in the right direction by telling me what I'm doing wrong, I'd very much appreciate that. Thanks in advance, once again. - Mi Quote Link to comment Share on other sites More sharing options...
Michan Posted November 8, 2007 Author Share Posted November 8, 2007 Topic not solved. :'( Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.