問題描述
我想為 php 網站開發 bbcode 過濾器.(我正在使用 cakephp,它將是一個 bbcode 助手)我有一些要求.
I'd like to work on a bbcode filter for a php website. (I'm using cakephp, it would be a bbcode helper) I have some requirement.
Bbcodes 可以嵌套.所以這樣的事情是有效的.
[block]
[block]
[/block]
[block]
[block]
[/block]
[/block]
[/block]
Bbcodes 可以有 0 個或多個參數.
例如:
[video: url="url", width="500", height="500"]Title[/video]
Bbcodes 可能有多種行為.
假設,[url]text[/url]
將被轉換為 [url:url="text"]text[/url]
或者視頻 bbcode 將能夠在 youtube、dailymotion 之間進行選擇......
Let say, [url]text[/url]
would be transformed to [url:url="text"]text[/url]
or the video bbcode would be able to choose between youtube, dailymotion....
我認為它滿足了我的大部分需求.我已經用正則表達式做了一些事情.但我最大的問題是匹配參數.事實上,我得到了嵌套的 bbcode 和帶有 0 個參數的 bbcode.但是當我為參數添加正則表達式匹配時,它沒有正確匹配嵌套的 bbcode.
I think it cover most of my needs. I alreay done something with regex. But my biggest problem was to match parameters. In fact, I got nested bbcode to work and bbcode with 0 parameters. But when I added a regex match for parameters it didn't match nested bbcode correctly.
"[($tag)(=.*)"](.*)[/1]"
//這不是 .* 而是非-grey 匹配器
"[($tag)(=.*)"](.*)[/1]"
// It wasn't .* but the non-gready matcher
我現在沒有完整的正則表達式,但我有類似的東西(上圖).
I don't have the complete regex with me right now, But I had something that looked like that(above).
那么有沒有一種方法可以將 bbcode 與正則表達式或其他東西有效匹配.我唯一能想到的是使用訪問者模式并以這種方式將我的文本與每個可能的標簽分開,我可以對我的文本解析有更多的控制,我可能會驗證我的文檔,如果輸入文本沒有'沒有有效的 bbcode.我可以在保存任何內容之前通知用戶有錯誤.
So is there a way to match bbcode efficiently with regex or something else. The only thing I can think of is to use the visitor pattern and to split my text with each possible tags this way, I can have a bit more of control over my text parsing and I could probably validate my document so if the input text doesn't have valid bbcode. I could Notify the user with a error before saving anything.
我會使用 sablecc 來創建我的文本解析器.http://sablecc.org/
I would use sablecc to create my text parser. http://sablecc.org/
有什么更好的主意嗎?或者任何可以導致高效靈活的 bbcode 解析器的東西?
Any better idea? or anything that could lead to a efficient flexible bbcode parser?
謝謝你,抱歉我的英語不好...
Thank you and sorry for my bad english...
推薦答案
pecl和 PEAR BBCode 解析庫.軟件已經夠難了,無需自己重新發明多年的工作.
There's both a pecl and PEAR BBCode parsing library. Software's hard enough without reinventing years of work on your own.
如果這些都不是一個選項,我會專注于將 BBCode 轉換為有效的 XML 字符串,然后使用您最喜歡的 XML 解析例程.這里的想法非常粗略,但是
If neither of those are an option, I'd concentrate on turning the BBCode into a valid XML string, and then using your favorite XML parsing routine on that. Very very rough idea here, but
通過 htmlspecialchars 運行代碼以轉義任何需要轉義的實體
Run the code through htmlspecialchars to escape any entities that need escaping
將所有 [ 和 ] 字符轉換為 <和 >分別
Transform all [ and ] characters into < and > respectively
不要忘記考慮 [tagname:
Don't forget to account for the colon in cases like [tagname:
如果 BBCode 嵌套正確,您應該設置為將此字符串傳遞到 XML 解析對象(SimpleXML、DOMDocument 等)
If the BBCode was nested properly, you should be all set to pass this string into an XML parsing object (SimpleXML, DOMDocument, etc.)
這篇關于解析 bbcode 的最佳方式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!