dongpu42006096 2012-03-25 17:30
浏览 6

php Pattern匹配自定义标签

I am trying to replace all the tags in a file like {{name:something}} to some certain values. Note that in {{name::something}} only something might warry. Like {{name:title}} or {{name:content}}. That "name" will always be there.

Anyway, I used preg_match_all and preg_replace, it all worked out. I found the matches with this regular expression: $patmatch="/{{name:[a-zA-Z0-9._-]+}}/";

My problem is the following:

When a tag like this does not match the pattern (it has a syntax error), like {{notname:something}} or {{name something}} or {{ }} i must throw an exception. How do i know if the tag is there, but with a syntax error?

Can i somehow make another pattern search to check if it's a tag (between {{ and }}) and if it's not like {{name:something}}?

Any help would be appreciated, Thanks!

  • 写回答

3条回答 默认 最新

  • dtn43447 2012-03-25 17:34
    关注

    Off the top of my head:

    if(strpos($text, '{{name:') === false)
    {
        //exception
    }
    else
    {
        //properly formatted text
    }
    

    Something like that?

    评论

报告相同问题?

悬赏问题

  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解