Jump to content

Need help with a pattern


Brandon Jaeger

Recommended Posts

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\" /]
Link to comment
Share on other sites

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]
<?php
preg_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
Link to comment
Share on other sites

[!--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
Link to comment
Share on other sites

[!--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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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!

---brandon

Edit: Maybe I have to clear the $string and $replace arrays before it gets to the posting part (after the topic part).
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.