I'm making my own forums and I don't wan开发者_运维问答t any BB code on it, but instead my own, so i've gotten [b][u][img] working etc.
But i'm having problems with [quote=1][/quote] where the number is the user id...
E.G lets say I quote someone
So once I submit my post: (The variable $post would be:) '[quote=1] Quoted post :P[/quote]'
How would I then get the number out the string? (But not the wrong number -not a number in the quoted post)
(So I could then use str_replace() to replace with a table which makes it looked quoted)
?? :)
\[quote=([0-9]*)\]
and grab the captured string $1
$pattern = "{\[quote=([0-9]*)\](.*)\[\/quote\]}";
$subject = $post;
preg_match($pattern, $subject, $matches);
//$matches[0] contains the whole string
//$matches[1] contains the id
It is very common to use regular expressions in order to implement BB-Codes. Of course you could use something like str_replace, but you will probably get some problems later.
Use the following pattern to make sure that the quote-tag gets also closed:
/\[quote=(\d+)\](.*?)\[\/quote\]/is
Now you should use preg_replace or preg_match to work with it.
For example:
echo preg_replace('/\[quote=(\d+)\](.*?)\[\/quote\]/is',
'<b>\\1 wrote:</b> \\2',
$input
);
Or:
$input = "text [quote=11]my quoted post
abc[/quote]
[quote=20]my quoted post 2[/quote]";
if(preg_match_all('/\[quote=(\d+)\](.*?)\[\/quote\]/is', $input, $matches)) {
var_dump($matches);
}
This should work for you.
$post = '[quote=1] Quoted post :P[/quote]';
if (preg_match("/\\[quote=([\d]+)\\]/",$post,$matches)) {
//echo "<pre>".print_r($matches,true)."</pre>";
$quote_user = $matches[1];
}
精彩评论