簡體   English   中英

PHP preg_replace_callback

[英]PHP preg_replace_callback

我在我的html文件中添加了這個標簽:

    {{block:my_test_block}} 
{{news:my_test_block2}}

我需要解析此標簽並將其替換為db中的內容,這是我的一種方式:

     ob_start();
     require_once('file.php');
     $template = ob_get_contents();
     ob_end_clean();

      $line = preg_replace_callback('/{(\w+)}/' , "parseTag" , $template);

function parseTag($matches){
   //here switch for block, news, 
}

這是正確的方法嗎? 謝謝。

嘗試使用

'/{{(.+):(.+)}}/'

因此,在$ matches [1]中,您將看到塊或新聞。

您的腳本應如下所示

$line = preg_replace_callback('/{{(.+):(.+)}}/' , "parseTag" , $template);

function parseTag($matches){
   if($matches[1] == 'block'){
        $return = 'Its Blocked';
   }elseif($matches[1] == 'news'){
        $return = 'Great news';
   }else{
        $return = 'Ops...';
   }
   return $return;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM