Brandon Jaeger Posted June 21, 2006 Share Posted June 21, 2006 What would the pattern be for something like this?[b]#QUOTE_POST_123#[/b]Where "123" is the post's id, which I'll need to grab out of that it.Thanks in advance!---brandon Quote Link to comment Share on other sites More sharing options...
effigy Posted June 21, 2006 Share Posted June 21, 2006 [code]<?php $string = '#QUOTE_POST_123#'; if (preg_match('/^#\w+?(\d+)#$/', $string, $matches)) { echo "id = $matches[1]"; }?>[/code] Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted June 21, 2006 Author Share Posted June 21, 2006 I have this and it doesn't find the matches:[code] if(preg_match('/^#QUOTE_TOPIC_([[:digit:]]+)#$/', $topic_body, $matches)) { $found = $matches[1]; $topic_body = preg_replace('/^#QUOTE_TOPIC_([[:digit:]]+)#$/' , $forum->find_quotes_and_replace($found , TOPIC) , $topic_body); $topic_body = preg_replace('/^#QUOTE_TOPIC_([[:digit:]]+)#$/' , '' , $topic_body); }[/code]It somewhat worked when I didn't have [b]/^[/b] and [b]$/[/b] in there. It only recognized [b]QUOTE_TOPIC_123[/b], not [b]#QUOTE_TOPIC_123[/b], though. [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /] Quote Link to comment Share on other sites More sharing options...
obsidian Posted June 21, 2006 Share Posted June 21, 2006 you may need to give us a little more info about the situation. if you're looking for a list of all the quotes in a block of text, the approach would be different than if it's simply matching a string to be sure it's in the correct format. if you are indeed checking for the quotes within a block of text, try this and see if it helps:[code]<?phppreg_match_all('|#QUOTE_TOPIC_(\d+)#|', $string, $matches, PREG_SET_ORDER);foreach ($matches as $match) echo "$match[0] value: $match[1]<br />\n";?>[/code]hope this helps Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted June 21, 2006 Author Share Posted June 21, 2006 Could you please show me how to integrate that into my current code (posted above)?---brandon Quote Link to comment Share on other sites More sharing options...
obsidian Posted June 21, 2006 Share Posted June 21, 2006 [!--quoteo(post=386382:date=Jun 21 2006, 09:29 AM:name=v3x)--][div class=\'quotetop\']QUOTE(v3x @ Jun 21 2006, 09:29 AM) [snapback]386382[/snapback][/div][div class=\'quotemain\'][!--quotec--]Could you please show me how to integrate that into my current code (posted above)?---brandon[/quote]i can try... lol. i think you'd use something like this:[code]<?php$strings = array();$replace = array();preg_match_all('|#QUOTE_TOPIC_(\d+)#|', $topic_body, $matches, PREG_SET_ORDER);foreach ($matches as $match) { // set up your arrays for your str_replace $strings[] = $match[0]; $replace[] = $forum->find_quotes_and_replace($match[1], TOPIC);}$topic_body = str_replace($strings, $replace, $topic_body);?>[/code]it may need a little tweaking, but i think this is what you're after.good luck Quote Link to comment Share on other sites More sharing options...
effigy Posted June 21, 2006 Share Posted June 21, 2006 [!--quoteo(post=386348:date=Jun 21 2006, 06:34 AM:name=v3x)--][div class=\'quotetop\']QUOTE(v3x @ Jun 21 2006, 06:34 AM) [snapback]386348[/snapback][/div][div class=\'quotemain\'][!--quotec--]I have this and it doesn't find the matches:[code] if(preg_match('/^#QUOTE_TOPIC_([[:digit:]]+)#$/', $topic_body, $matches)) { $found = $matches[1]; $topic_body = preg_replace('/^#QUOTE_TOPIC_([[:digit:]]+)#$/' , $forum->find_quotes_and_replace($found , TOPIC) , $topic_body); $topic_body = preg_replace('/^#QUOTE_TOPIC_([[:digit:]]+)#$/' , '' , $topic_body); }[/code]It somewhat worked when I didn't have [b]/^[/b] and [b]$/[/b] in there. It only recognized [b]QUOTE_TOPIC_123[/b], not [b]#QUOTE_TOPIC_123[/b], though. [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /][/quote][[:digit:]] is for ereg, not preg. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted June 21, 2006 Author Share Posted June 21, 2006 obsidian: It works great! Thanks! Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted June 21, 2006 Author Share Posted June 21, 2006 Another problem - What's wrong with this?[code] $strings = array(); $replace = array(); preg_match_all('|#QUOTE_TOPIC_(\d+)#|', $post_body, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $strings[] = $match[0]; $replace[] = $str; } $post_body = str_replace($strings, $replace, $post_body); preg_match_all('|#QUOTE_POST_(\d+)#|', $post_body, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $strings[] = $match[0]; $replace[] = $str; } $post_body = str_replace($strings, $replace, $post_body);[/code] Quote Link to comment Share on other sites More sharing options...
obsidian Posted June 21, 2006 Share Posted June 21, 2006 for one thing, you're replacing every match you find with the variable $str without ever changing what that variable is equal to. for another thing, we don't know what the error is ;-)it's hard to say what's wrong with code without knowing what it's [i]supposed[/i] to do and what it is [i]actually[/i] doing. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted June 21, 2006 Author Share Posted June 21, 2006 It's supposed to go through a post and find any #QUOTE_TOPIC_{ID}# string, search for a match in the db, then replace it with that post.Here's the full function:[code] function find_quotes_in_post($id , $post_body , $type = NULL) { if($type != NULL) { $query = "SELECT * FROM forum_" . (($type == TOPIC) ? "topics" : "posts") . " WHERE id = '" . $id . "'"; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result) > 0) { $row = mysql_fetch_assoc($result); $poster_id = $row["posterid"]; $body = $row["body"]; $stamp = $row["timestamp"]; $date = date("F j, Y, g:i a" , $stamp); $str .= "<div class=\"quote\">"; $str .= "<img src=\"images/paper_folded2.gif\"> Posted by <b><a href=\"#\">" . name($poster_id) . "</a></b> - " . $date . "<br \><br \>"; $str .= $body; $str .= "</div>"; } $strings = array(); $replace = array(); preg_match_all('|#QUOTE_TOPIC_(\d+)#|', $post_body, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $strings[] = $match[0]; $replace[] = $str; } $post_body = str_replace($strings, $replace, $post_body); preg_match_all('|#QUOTE_POST_(\d+)#|', $post_body, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $strings[] = $match[0]; $replace[] = $str; } $post_body = str_replace($strings, $replace, $post_body); return $post_body; } } }[/code]It quotes each one like 20+ times. I have no idea what's wrong with it. If anyone wants to make a better function for this feature, please feel free to do so.If you need to see more code, just ask!---brandonEdit: Maybe I have to clear the $string and $replace arrays before it gets to the posting part (after the topic part). Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 22, 2006 Share Posted June 22, 2006 Why not just do something like this:[code] $replacements = array(); /* Load this array with the replacement text, indexed by the number you want to replace. I.e., $replacement[123] = "the replacement text..." I'm sure you can figure that out. Then use preg_replace to replace the tags with the text. */ preg_replace('/#QUOTE_(?:TOPIC|POST)(\d+)#/e','$replacements[$1]',$post_body);[/code] 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.