Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

93 users online



Regex pattern help

Regex pattern help

Currently viewing this thread: 1 (0 members and 1 guests)


v3x0rg

v3x0rg

Pawn Coder
Status: Offline!

Regex pattern help

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

___________________

- Moderator at the official AMX Mod X forums

Noel

Noel

Nobody fucks with my title.
Status: Offline!

PCRE: #QUOTE_POST_(\d+)#
POSIX: #QUOTE_POST_([[:digit:]]+)#

Something like that.

___________________

<3

v3x0rg

v3x0rg

Pawn Coder
Status: Offline!

Hmm,

It's not recognizing the #'s. If I put QUOTE_POST_123 it still works.

Edit: Here's my current code:

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


With the /^ and $/ it's not recognizing it at all.

___________________

- Moderator at the official AMX Mod X forums

Last edited by v3x0rg, June 21st, 2006 04:51 PM (Edited 1 times)

v3x0rg

v3x0rg

Pawn Coder
Status: Offline!

Nevermind, I figured it out Smile

___________________

- Moderator at the official AMX Mod X forums

ringpull

ringpull

Neversidian
Status: Offline!

v3x0rg: Mind posting what you did so other future users who may come across this thread will know a solution? Smile

erod

erod

uh yea
Status: Offline!
PHP:

<?php

/**
 * A Valid pattern to match the digits of a string and ONLY  the digits, if preg_match_all is used, the search will be repeated through the string.
 */
// '/([[:digit:]]+)/'

// Explanation
'/
    ( # Begin our sub patern
        [ # Begin our character class
                [:digit:] # We are only conerned about digits
            ] # End our character class
        + # add the + quantifier to match as many digits asneeded
    ) # End our sub pattern
/'

?>

Not tested but quality assured Tongue just a explanation for other forum posters and im bored

___________________

I code alot.

BigToach

BigToach

Neversidian
Status: Offline!

([0-9]+)

___________________

Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!

v3x0rg

v3x0rg

Pawn Coder
Status: Offline!

ringpull: Here's my code:

PHP:

<?php
        
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());

                
$str "";

                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\">&nbsp;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$matchesPREG_SET_ORDER);
                foreach(
$matches as $match
                {
                    
$strings[] = $match[0];
                    
$replace[] = $str;
                }

                
$post_body str_replace($strings$replace$post_body);

                unset(
$strings);
                unset(
$replace);

                
$strings = array();
                
$replace = array();

                
preg_match_all('|#QUOTE_POST_(\d+)#|'$post_body$matchesPREG_SET_ORDER);
                foreach(
$matches as $match
                {
                    
$strings[] = $match[0];
                    
$replace[] = $str;
                }

                
$post_body str_replace($strings$replace$post_body);

                return 
$post_body;
            }
        }
    }
?>


I sort of gave up on this idea though. If you want to suggest any changes to my code, please do so Wink

P.S. I don't think it works 100% though.

___________________

- Moderator at the official AMX Mod X forums

Noel

Noel

Nobody fucks with my title.
Status: Offline!
Originally posted by v3x0rg:

Hmm,

It's not recognizing the #'s. If I put QUOTE_POST_123 it still works.

Edit: Here's my current code:

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


With the /^ and $/ it's not recognizing it at all.

The ##'s were delimiters for the regex, I should have been more specific.

PHP:

<?php

preg_replace
('#^QUOTE_TOPIC_(\d+)$#'$forum->find_quotes_and_replace($foundTOPIC), $topic_body);

?>

preg == PCRE
ereg == POSIX

http://php.net/pcre

Read up, mate.

___________________

<3

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0112 seconds.