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
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:
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.
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.
preg_match_all('~\[quote\]([0-9]*)\[/quote\]~i', $comment, $matches); // $matches[1] will hold the number only PHP: (Note the parenthesis.)
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:
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.
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.
$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: