Jump to content

Archived

This topic is now archived and is closed to further replies.

Brandon Jaeger

Need help with a pattern

Recommended Posts

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

Share this post


Link to post
Share on other sites
[code]<?php
    $string = '#QUOTE_POST_123#';
    if (preg_match('/^#\w+?(\d+)#$/', $string, $matches)) {
        echo "id = $matches[1]";
    }
?>[/code]

Share this post


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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
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).

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.