2013-08-19 2 views
0

온라인으로 곱슬 파서를 생각해 보았습니다. 더 발전시키고 싶습니다. 원본 파서는 youtube를 전달하기 위해 만들어졌습니다. 작동php : 곱슬 파서 작성하기

class curly 
{ 
    /** 
    * Replace the macros in an input string 
    * @param string $input 
    * @return string 
    */ 
    public function replace ($input) { 
     //return preg_replace("/\{{2}([a-z]+\|.+)\}{2}/Ue",'$this->_replace("\\1")',$input); // original 
     return preg_replace("/\{{2}(([a-z]+\|.+)|([a-z\_]))\}{2}/Ue",'$this->_replace("\\1")',$input); 
    } 

    /** 
    * Run the replacement code on a given macro string 
    * @param string $input 
    * @return string 
    */ 
    private function _replace ($input) { 

     print_r($input); 

     list ($name,$params) = explode("|",$input); 

     if (method_exists($this,$name)) { 
      return $this->$name($params); 
     }else{ 
      if($input === 'email_compnay') return '[email protected]'; 
       else if($input === 'email_personal') return '[email protected]'; 
     } 

     throw new Exception ("Unrecognised macro: {$name}.",500); 
    } 

    /** 
    * Replaces a YouTube curly 
    * @param string $params 
    * @return string 
    */ 
    private function youtube ($params) { 
     parse_str($params); 

     // set defaults 
     if (!isset($id)) { $id = "ykwqXuMPsoc"; } 
     if (!isset($width)) { $width = 560; } 
     if (!isset($height)) { $height = 315; } 

     // output the final HTML 
     return "<iframe width=\"{$width}\" height=\"{$height}\" src=\"http://www.youtube.com/embed/{$id}\" frameborder=\"0\" allowfullscreen></iframe>"; 
    } 
} 

예를 들어, 사용,

$curly = new curly(); 
$input = '<p>{{youtube|id=ykwqXuMPsoc&width=560&height=315}}</p>'; 
echo $curly->replace($input); 
// return <p><iframe width="560" height="315" src="http://www.youtube.com/embed/ykwqXuMPsoc" frameborder="0" allowfullscreen></iframe></p> 

작동하지 않는 또 다른 예, 그것은 {{email_company}}

$input = '<p>{{email_company}}</p>'; 
echo $curly->replace($input); 
// return <p>{{email_company}}</p> 

다음은 [email protected]를 반환해야하지만 대신 나에게 {{email_company}}을 제공합니다 .. .

이 정규식에서 힌지가 맞지 않습니까? 이중 중괄호에서 ID = ykwqXuMPsoc & 폭 = 560 & 높이 = 315 |

"/\{{2}(([a-z]+\|.+)|([a-z\_]))\}{2}/Ue" 
+0

입니다. 나는 PHP를 모르지만 정확히 달성하려고하는 것이 무엇인지 알고 있다면 정규 표현식을 도울 수있을 것입니다. 덕분에 –

+0

. 이중 중괄호에서'email_company' 또는'youtube | id = ykwqXuMPsoc & width = 560 & height = 315'를 얻을 수있는 정규식이 필요하다고 생각합니다. 그것은 가능한가? – laukok

+0

현재 regex는'email_company'가 아닌'youtube | id = ykwqXuMPsoc & width = 560 & height = 315'만을 가져옵니다 ... – laukok

답변

2

은 내가 email_company 또는 YouTube를 잡아 할 수있는 정규식을 필요가 있다고 생각합니다. 그것은 가능한가?

예 가능합니다. 이 정규식은 \{{2}([^}]*)\}{2} 일 뿐이며 Regex 101 Demo에서 확인할 수 있습니다.

어떤 식 으로든이 표현이 마음에 들지 않거나 귀하의 의견에 당신이 말한대로하지 않았다면 (이유가 무엇입니까?) 다음에 게시 한 원본 표현을 수정할 수 있습니다. 하나는 모든 쉘 작업 벌금을 다음 여기

/\{{2}(([a-z]+\|.+)|([a-z\_]+))\}{2}/Ue 
//      ^
//       | 
//     Notice the added + sign, your previous expression only 
//     matched a single character. 

을 그리고 것은 미안하지만 정확히 뭘하려고하는 어떤 Regex 101 Demo

+0

감사합니다.'+'추가하고 완벽하게 작동합니다! 덕분에 너무 많이! – laukok

+0

나는'\ {{2 } [{^}] *) \} {2}'그러나 PHP에서는 작동하지 않습니다. 이유는 ... – laukok

+0

언젠가 문자열에 중괄호가 다시 나타납니다 - '

{{email_company}} { {email_personal}}

' – laukok