Jump to content


Photo

Need help with a pattern


  • Please log in to reply
11 replies to this topic

#1 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 21 June 2006 - 02:51 AM

What would the pattern be for something like this?

#QUOTE_POST_123#

Where "123" is the post's id, which I'll need to grab out of that it.

Thanks in advance!

---brandon
PHP is my friend (=

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 21 June 2006 - 04:23 AM

<?php
    $string = '#QUOTE_POST_123#';
    if (preg_match('/^#\w+?(\d+)#$/', $string, $matches)) {
        echo "id = $matches[1]";
    }
?>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 21 June 2006 - 11:34 AM

I have this and it doesn't find the matches:
            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);
            }
It somewhat worked when I didn't have /^ and $/ in there. It only recognized QUOTE_TOPIC_123, not #QUOTE_TOPIC_123, though.

[img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]
PHP is my friend (=

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 June 2006 - 12:10 PM

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:
<?php
preg_match_all('|#QUOTE_TOPIC_(\d+)#|', $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) echo "$match[0] value: $match[1]<br />\n";
?>

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 21 June 2006 - 01:29 PM

Could you please show me how to integrate that into my current code (posted above)?

---brandon
PHP is my friend (=

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 June 2006 - 01:42 PM

[!--quoteo(post=386382:date=Jun 21 2006, 09:29 AM:name=v3x)--][div class=\'quotetop\']QUOTE(v3x @ Jun 21 2006, 09:29 AM) View Post[/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:
<?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);
?>
it may need a little tweaking, but i think this is what you're after.

good luck
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 21 June 2006 - 01:59 PM

[!--quoteo(post=386348:date=Jun 21 2006, 06:34 AM:name=v3x)--][div class=\'quotetop\']QUOTE(v3x @ Jun 21 2006, 06:34 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I have this and it doesn't find the matches:
            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);
            }
It somewhat worked when I didn't have /^ and $/ in there. It only recognized QUOTE_TOPIC_123, not #QUOTE_TOPIC_123, 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.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#8 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 21 June 2006 - 02:22 PM

obsidian: It works great! Thanks!
PHP is my friend (=

#9 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 21 June 2006 - 03:10 PM

Another problem - What's wrong with this?
                $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);

PHP is my friend (=

#10 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 June 2006 - 04:30 PM

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 supposed to do and what it is actually doing.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#11 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 21 June 2006 - 11:55 PM

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:
        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;
            }
        }
    }
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).
PHP is my friend (=

#12 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 22 June 2006 - 02:54 PM

Why not just do something like this:

    $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);

Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users