2012-02-11 3 views
2

PHP로 함수를 작성하려고하는데,이 함수는 mySQL 데이터베이스를 통해 반복되고 모든 조건부 주석을 삭제합니다.다중 회선 조건문을 preg_replace하는 PHP 함수

이 같은 모습을 대체 할 텍스트 :

<!--[if gte mso 9]><xml> <o:OfficeDocumentSettings> <o:AllowPNG /> </o:OfficeDocumentSettings> </xml><![endif]--><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves /> <w:TrackFormatting /> <w:DoNotShowRevisions /> <w:DoNotPrintRevisions /> <w:DoNotShowMarkup /> <w:DoNotShowComments /> <w:DoNotShowInsertionsAndDeletions /> <w:DoNotShowPropertyChanges /> <w:PunctuationKerning /> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF /><![endif]-->

것은 여기 내 코드는 아무것도 일치하지 않는 것

$content = array('1' => $my_text_with_conditional_quotes) 

foreach($content as $id => $v){ 
    print $v .' <br>'; 
    $str = addcslashes(preg_replace("/<!(--)?(?=\[)(?:(?!<!\[endif\]\1>).)*<!\[endif\]\1>/s",'',$v)); 
    print $str . '<br>'; 
    print $id . '<br>'; 
    exit; 
} 

입니다. 나는 무엇을 놓치고 있습니까?

답변

1

캡처 그룹

"/<!(--)?(?=\[)(?:(?!<!\[endif\]\\1>).)*<!\[endif\]\\1>/s" 
+0

오류를 잡아 주셔서 감사합니다. 작동 중입니다. – alockwood05

+0

@AlexLockwood : 천만에요. – Toto

0
여기에서 와일드 카드 기간 이동

: 1>).)* 여기에 : 1>)).*

또한, RegexPal들이 일치 무엇을보고, 라이브 귀하의 정규 표현식에를 테스트 굉장합니다.

+0

글쎄, 좋은 결과는 있지만 결과는 바뀌지 않습니다. 그럼에도 불구하고 응답에 감사드립니다. – alockwood05

0

XML을 구문 분석하는 정규 표현식을 사용하지 않습니다를 참조 할 \ 탈출 따옴표

'/<!(--)?(?=\[)(?:(?!<!\[endif\]\1>).)*<!\[endif\]\1>/s' 

' 또는 더블로 정규 표현식을 묶습니다. PHP 문자열 함수를 사용하여이 문제를 해결할 수 있습니다. 다음과 같이됩니다 :

while (1==1) { 

    $openingTagOffset = strpos($field, "<!--[if gte mso 9]>"); 
    if ($openingTagOffset === false) break; 
    $closingTag = "<![endif]-->"; 
    $closingTagOffset = strpos($field, $closingTag, $openingTagOffset); 
    $field = substr_replace($field, "", $openingTagOffset, $closingTagOffset - $openingTagOffset + strlen($closingTag)); 

} 
+0

감사합니다. 좋은 생각이 좋습니다. 이것은 내가 다른 문제를보다 효율적으로 해결하는 데 도움이 될 것이다. (정규 표현식이 더 많은 시스템 자원을 사용한다고 생각한다.) 그러나 시작 태그와 끝 태그가 정확히 무엇인지 확실하지 않았습니다. 그리고 제가 알고있는 한 정규 표현식은 패턴 일치를 수행하는 방법입니다. – alockwood05

관련 문제