php Preg Match Expression

Discussion in 'PHP' started by Silver89, Jul 31, 2008.

  1. #1
    Hi,

    I'm trying to take the user input from a form, scan the comment for:

    [quote]12[/quote]
    HTML:
    And then perform a mysql query with that Id numbder.

    However I'm struggling getting the preg_match to find the number, here's what I have.

    I realise it's not secure yet but I'm just testing at the moment.
    
    	preg_match_all('~[quote][0-9][/quote]~i', $comment, $matches);
    		
    		foreach ($matches[1] AS $match)
    		{
    			if($match != '')
    			{
    			$result = mysql_query("SELECT message FROM forumPosts WHERE id='$match'");
    			$row = mysql_fetch_row($result);
    			$comment = "$row[0]<br />$match<br />$comment" ;
    			}
    		}
    
    PHP:
    I think the expression in the Preg_match may be wrong?

    Thanks
    Dan
     
    Silver89, Jul 31, 2008 IP
  2. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    First thing I noticed - it won't match 12 because you've only allowed for the numbers 0 - 9, to get it to match double digit numbers you'll need to put [0-9][0-9]. To have it match unlimited numbers of digits, put [0-9]*

    Also, you need to delimit the [ and ] characters that aren't meant to contain an expression, put a backspace before them.

    You also need to change 'foreach ($matches[1] AS $match)' to 'foreach ($matches[0] AS $match)', unless you only want it to work for the second quote tag.
    preg_match_all('~\[quote\][0-9][0-9]\[/quote\]~i', $comment, $matches);
           
            foreach ($matches[1] AS $match)
            {
                if($match != '')
                {
                $result = mysql_query("SELECT message FROM forumPosts WHERE id='$match'");
                $row = mysql_fetch_row($result);
                $comment = "$row[0]<br />$match<br />$comment" ;
                }
            }
    PHP:
    Or to have it match unlimited numbers of digits:
    preg_match_all('~\[quote\][0-9]*\[/quote\]~i', $comment, $matches);
           
            foreach ($matches[1] AS $match)
            {
                if($match != '')
                {
                $result = mysql_query("SELECT message FROM forumPosts WHERE id='$match'");
                $row = mysql_fetch_row($result);
                $comment = "$row[0]<br />$match<br />$comment" ;
                }
            }
    PHP:
    If you mean to have it display all of the quote tags entered, you need to put something like this:
    preg_match_all('~\[quote\][0-9]*\[/quote\]~i', $comment, $matches);
           
            foreach ($matches AS $matches_mini)
            {
                    foreach ($matches_mini AS $match)
                    {
                        if($match != '')
                        {
                        $result = mysql_query("SELECT message FROM forumPosts WHERE id='$match'");
                        $row = mysql_fetch_row($result);
                        $comment .= "$row[0]<br />$match<br />$comment<br />" ;
                        }
                    }
            }
            }
    PHP:
     
    Pos1tron, Jul 31, 2008 IP
  3. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #3
    Thanks for that this is great help, However this returns a match of:

    [quote]12[/quote]
    HTML:
    I could just strip the quote tags from the match but if it's possible to match without the quote tags that would help greatly.

    Thanks in advance.
     
    Silver89, Jul 31, 2008 IP
  4. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I expect it's possible, but I'm not sure how really, without just matching any numbers at all. As you say, for now just strip the tags using a custom function doing str_replace and then array_map -ping that to $matches.
     
    Pos1tron, Aug 1, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    
    preg_match_all('~\[quote\]([0-9]*)\[/quote\]~i', $comment, $matches);
    
    // $matches[1] will hold the number only
    
    PHP:
    (Note the parenthesis.)
     
    nico_swd, Aug 1, 2008 IP
    Silver89 likes this.
  6. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #6
    Ok thanks again,

    Now i'm seeing another problem, if a user inputs:

     [quote]12[/quote] [quote]13[/quote] [quote]14[/quote]
    HTML:
    Then it will just return:

    <q>User:<span>Comment</span></q>
    HTML:
    and then not get 13 or 14, but I need to make a loop so it builds the comment up,

    I know the following won't work but it's just showing the basic idea of how I need it too function.

    $comment = $match[0] + [$match[1];
    PHP:

    The current code i'm using is as follows:

    		preg_match_all('~<q>([0-9]*)</q>~i', $comment, $matches);
    		foreach ($matches[1] AS $match)
    		{
    			if($match != '')
    			{
    			$result = mysql_query("SELECT message, username FROM forumPosts WHERE id='$match'");
    			$row = mysql_fetch_row($result);
    		
    			$comment = "<q><span>$row[1]:</span> $row[0]</q>" ;
    			}
    		}
    PHP:
     
    Silver89, Aug 1, 2008 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    
    preg_match_all('~<q>(\d+)</q>~i', $comment, $matches);
    $ids = array_unique($matches[1]);
    
    $query = mysql_query("SELECT `message`, `username` FROM `forumPosts` WHERE `id` IN(" . implode(', ', $ids) . ")") OR die(mysql_error());
    $comment = '';
    
    while ($row = mysql_fetch_row($query))
    {
    	$comment .= "<q><span>$row[1]:</span> $row[0]</q>";
    }
    
    echo $comment;
    
    PHP:
    Faster and shorter.
     
    nico_swd, Aug 1, 2008 IP
    lordadel likes this.
  8. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #8
    Ok, is there a way to only do everything after the perg_match if there is a match?

    else just skip the rest and continue on.
     
    Silver89, Aug 1, 2008 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    
    $comment = '';
    
    if (preg_match_all('~<q>(\d+)</q>~i', $comment, $matches))
    {
        $ids = array_unique($matches[1]);
    
        $query = mysql_query("SELECT `message`, `username` FROM `forumPosts` WHERE `id` IN(" . implode(', ', $ids) . ")") OR die(mysql_error());
    
        while ($row = mysql_fetch_row($query))
        {
            $comment .= "<q><span>$row[1]:</span> $row[0]</q>";
        }
    
        echo $comment;
    }
    
    PHP:
     
    nico_swd, Aug 1, 2008 IP