Chaos23 Posted July 14, 2009 Share Posted July 14, 2009 Basically I want to convert this: <!--quoteo(post={PID}:date={DATE}:name={NAME})--> <div class='quotetop'>QUOTE({USER} @ {DATE}) <a href="index.php?act=findpost&pid={PID}"><img src='snapback.gif' alt='*' border='0' /></a></div> <div class='quotemain'><!--quotec-->{POST}<!--QuoteEnd--></div> <!--QuoteEEnd--> to this: <div class='quotemain'> Quote: {USER} @ {DATE} <a href="index.php?act=findpost&pid={PID}"><img src="snapback.gif" alt='*' border='0' /></a> <hr /> {POST} </div> I didn't know wether preg_replace, or ereg_replace would be better, all the regex stuff is still confusing to me. Quote Link to comment https://forums.phpfreaks.com/topic/165917-preg_match/ Share on other sites More sharing options...
Adam Posted July 14, 2009 Share Posted July 14, 2009 I'm assuming {PID}, {DATE}, etc. Will be switched for actual data? Could we see a few examples? At the moment can't really give an accurate enough solution due to too many possibilities, well you could but adding in real data could break it. For example what format is the date? Can "user" contain spaces? etc. Quote Link to comment https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875150 Share on other sites More sharing options...
Chaos23 Posted July 14, 2009 Author Share Posted July 14, 2009 Yeah, I didn't think about that. Here is an actual example. I just need to pull out the {PID}, {DATE}, {NAME} & {POST}, so that I can do a DB query and re enter the data into the new format, I am using a DB query to determine what group a member is in, cause certain groups have different color posts, and I want the quotes to match the original posters colors. <!--quoteo(post=279:date=Nov 26 2008, 09:38 PM:name=Chaos23)--> <div class='quotetop'>QUOTE(Chaos23 @ Nov 26 2008, 09:38 PM) <a href="index.php?act=findpost&pid=279"><img src='images/snapback.gif' alt='*' border='0' /></a></div> <div class='quotemain'><!--quotec-->I didn't copy it I quoted it<!--QuoteEnd--></div> <!--QuoteEEnd--> Quote Link to comment https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875154 Share on other sites More sharing options...
Adam Posted July 14, 2009 Share Posted July 14, 2009 Why don't you store the data in seperate fields, to prevent future issues like this? You can just format the post when you return it that way. To make things a little simpler I returned the PID, DATE & NAME together then the POST separately: <?php $str ='<!--quoteo(post=279:date=Nov 26 2008, 09:38 PM:name=Chaos23)--> <div class=\'quotetop\'>QUOTE(Chaos23 @ Nov 26 2008, 09:38 PM) <a href="index.php?act=findpost&pid=279"><img src=\'images/snapback.gif\' alt=\'*\' border=\'0\' /></a></div> <div class=\'quotemain\'><!--quotec-->I didn't copy it I quoted it<!--QuoteEnd--></div> <!--QuoteEEnd-->'; preg_match('#<!--quoteo\(post=([^:]+):date=([^:]+):name=([^\)]+)\)-->#', $str, $details); preg_match('#<!--quotec-->(.+?)<!--QuoteEnd-->#', $str, $message); print_r($details); print_r($message); ?> Should think you'll be okay from here? Quote Link to comment https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875180 Share on other sites More sharing options...
Chaos23 Posted July 14, 2009 Author Share Posted July 14, 2009 Thanks, That works perfectly. There is one other thing that I ran into. If a post has more than one quote in it uses the details from the first quote on both, what string could I use to kill the first one or could I use while or foreach to make each quote seperate. Quote Link to comment https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875191 Share on other sites More sharing options...
Adam Posted July 14, 2009 Share Posted July 14, 2009 You should be able to use preg_match_all(), but what's an example of a post with more than one quote? Quote Link to comment https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875194 Share on other sites More sharing options...
Chaos23 Posted July 14, 2009 Author Share Posted July 14, 2009 This is with more than one quote in a single post. <!--quoteo(post=279:date=Nov 26 2008, 09:38 PM:name=Chaos23)--> <div class='quotetop'>QUOTE(Chaos23 @ Nov 26 2008, 09:38 PM) <a href="index.php?act=findpost&pid=279"><img src='images/post_snapback.gif' alt='*' border='0' /></a></div> <div class='quotemain'><!--quotec-->I didn't copy it I quoted it.<!--QuoteEnd--></div> <!--QuoteEEnd--> <br /> What I meant was copy/paste the text. <br /> <!--quoteo(post=276:date=Nov 26 2008, 09:02 PM:name=Chaos23)--> <div class='quotetop'>QUOTE(Chaos23 @ Nov 26 2008, 09:02 PM) <a href="index.php?act=findpost&pid=276"><img src='images/post_snapback.gif' alt='*' border='0' /></a></div> <div class='quotemain'><!--quotec-->If he returns the project really should be wholely his and he can keep or abandon any changes made.<!--QuoteEnd--></div> <!--QuoteEEnd--> <br /> At the time I first read that I didn't know you were quoting it. Quote Link to comment https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875202 Share on other sites More sharing options...
Adam Posted July 14, 2009 Share Posted July 14, 2009 If you replace the preg_match() functions with preg_match_all() and take a look at the difference in the data returned, you should be able to spot how you can use it for 2, 3, 4, etc. different quotes in a single post. Quote Link to comment https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875212 Share on other sites More sharing options...
Chaos23 Posted July 15, 2009 Author Share Posted July 15, 2009 Here is the entire function something is missing that makes the quotes repeat themselves. function get_quote($post) { global $tcn, $DB, $std, $print; preg_match('#<!--quoteo\(post=([^:]+):date=([^:]+):name=([^\)]+)\)-->#', $post, $details); preg_match('#<!--quotec-->(.+?)<!--QuoteEnd-->#', $post, $message); $quote_top = $details[3].' @ '.$details[2]; if( $quote_top == ' @ ') { $quote_top = 'Quote:'; } else { $quote_top = 'Quote: '.$quote_top; } $post = preg_replace('#<!--quoteo(.+?)-->(.+?)<!--QuoteEnd-->#', '<div class=\'quotemain\'>'.$quote_top.'<hr />'.$message[0], $post); return $post; } Quote Link to comment https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-875523 Share on other sites More sharing options...
Chaos23 Posted July 18, 2009 Author Share Posted July 18, 2009 I tried the same thing with preg_match_all and got the same result different array, but still repeating. Quote Link to comment https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-877473 Share on other sites More sharing options...
Chaos23 Posted July 21, 2009 Author Share Posted July 21, 2009 This is the function with preg_match_all: function get_quote($post) { global $tcn, $DB, $std, $print; preg_match_all('#<!--quoteo\(post=([^:]+):date=([^:]+):name=([^\)]+)\)-->#', $post, $details); preg_match_all('#<!--quotec-->(.+?)<!--QuoteEnd-->#', $post, $message); for ($i=0; $i < count($message[0]); $i++) { $post = str_replace('<!--quotec-->', '', $post); $post = str_replace('<!--QuoteEnd-->', '', $post); $quote_top = $details[3][$i].' @ '.$details[2][$i]; if( $quote_top == ' @ ') { $quote_top = 'Quote:'; } else { $quote_top = 'Quote: '.$quote_top; } $post = preg_replace('#<!--quoteo(.+?)-->(.+?)<!--QuoteEEnd-->#', '<div class=\'quotemain\'>'.$quote_top.'<hr />'.$message[0][$i].'</div>', $post); } return $post; } This function works with individual quotes, or ones with only one snapback like this: <!--quoteo(post=279:date=Nov 26 2008, 09:38 PM:name=Chaos23)--> <div class='quotetop'>QUOTE(Chaos23 @ Nov 26 2008, 09:38 PM) <a href="index.php?act=findpost&pid=279"><img src='images/post_snapback.gif' alt='*' border='0' /></a></div> <div class='quotemain'><!--quotec-->I didn't copy it I quoted it.<!--QuoteEnd--></div> <!--QuoteEEnd--> <br /> What I meant was copy/paste the text. <br /> <!--quoteo--> <div class='quotetop'>QUOTE</div> <div class='quotemain'><!--quotec-->If he returns the project really should be wholely his and he can keep or abandon any changes made.<!--QuoteEnd--></div> <!--QuoteEEnd--> <br /> At the time I first read that I didn't know you were quoting it. But not ones with multiple snapbacks. All posts that have more than one. <!--quoteo(post={PID}:date={DATE}:name={USER})--> will repeat both the details and message throughout the main post that the quotes are contained in. Quote Link to comment https://forums.phpfreaks.com/topic/165917-preg_match/#findComment-879613 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.