온라인으로 곱슬 파서를 생각해 보았습니다. 더 발전시키고 싶습니다. 원본 파서는 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"
입니다. 나는 PHP를 모르지만 정확히 달성하려고하는 것이 무엇인지 알고 있다면 정규 표현식을 도울 수있을 것입니다. 덕분에 –
. 이중 중괄호에서'email_company' 또는'youtube | id = ykwqXuMPsoc & width = 560 & height = 315'를 얻을 수있는 정규식이 필요하다고 생각합니다. 그것은 가능한가? – laukok
현재 regex는'email_company'가 아닌'youtube | id = ykwqXuMPsoc & width = 560 & height = 315'만을 가져옵니다 ... – laukok