2012-04-08 2 views
0

[quote][/quote]에 싸여있는 텍스트가 있는데이 태그 앞에 모든 텍스트, 그 태그 사이의 모든 문자 및 그 태그 뒤에있는 모든 문자를 일치 시키려고합니다. 잡아 냄새는 여러 번 발생할 수 있지만 서로는 존재할 수 없다는 것입니다.preg_match_all 이상한 메시지

이유는 태그가 여러 개인 경우에도 태그 외부의 모든 텍스트에 필터를 적용하기 때문입니다.

이 내가 작업을 시작하고 무엇을 :

preg_match_all("/(^.*)\[quote\](.*?)\[\/quote\](.*)/si", $reply['msg'], $getthequotes); 

다음은 출력입니다 :

Array 
(
[0] => Array 
    (
     [0] => putting some stuff before the quote 
[quote][b]Logan said[/b][br]testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote] 

yep 

http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 

adding a quote 

[quote][b]Logan said[/b][br]This is the start of the second quote http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote] 

[i]04/07/12 20:18:07: Edited by Logan(2)[/i] 
    ) 

[1] => Array 
    (
     [0] => putting some stuff before the quote 

[quote][b]Logan said[/b][br]testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote] 

yep 

http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 

adding a quote 


    ) 

[2] => Array 
    (
     [0] => [b]Logan said[/b][br]This is the start of the second quote http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i] 
    ) 

[3] => Array 
    (
     [0] => 

[i]04/07/12 20:18:07: Edited by Logan(2)[/i] 
    ) 

) 

당신이 원하는 결과를 얻기 아니에요 볼 수 있듯이. 어떤 도움을 주시면 감사하겠습니다.

+0

아 ... HTML이 아닌 마크 업 언어 - 확실히 마지막으로 올바른 도구가 될 것입니다 정규 표현식에? –

+0

HTML로 파싱되는 태그와 같은 사용자 정의 bbcode가 있습니다. 모든 정규식 파싱은 PHP로 수행됩니다. –

+1

죄송합니다. 나는이 극단적 인 착각에 비추어 약간 냉소적이었습니다. (http://stackoverflow.com/a/1732454/596781). 대답은 정규 표현식을 사용하지 않는다는 것입니다. 올바른 도구가 아니기 때문입니다. –

답변

1

나는 이것을 시험하지 않았지만, [quote] 전에 그리고 이후에 물건을 원한다면, 시작 인용 태그의 첫 번째 발생에 대해 strpos를 수행 할 수 있습니다. 이제는 이전에 모든 것이 인용되지 않는다는 것을 알고 있습니다.

다음으로 첫 번째 일치하는 따옴표 태그의 인덱스에서 시작하여 strpos를 사용하여 닫는 따옴표 태그를 찾을 수 있습니다. 이 물건들을 버릴 수 있습니다.

이제 방금 찾은 마감 견적 태그의 시작 위치를 사용하여 다음 견적 블록에 대해 다른 strpos를 수행하십시오. 당신이 끝날 때까지 이것을 반복 할 수 있습니다.

+0

또한 네 스팅을 원한다면 첫 번째'[/ quote]'를 먼저 찾은 다음 여백'[quote]'를 찾기 위해 * 뒷쪽 *을 검색하면 가장 안쪽의 따옴표를 얻을 수 있습니다. 필요에 따라 포맷하고 다시 헹구고 반복하십시오. – mpen

+0

나는 그것 모두를 필요로 할 것이다. 비 인용 텍스트에서 추가 처리 만하면됩니다. 나는 그것을 할 수있는 것 같아요 각각의 자체 var에 다음 다시 다시 함께 모든 부분을 연결 ... 저장 엉덩이 거꾸로,하지만 그것은 작동 할 것 같아요. –

+0

예, 파트를 다시 연결하면 작동합니다. 미안합니다. 그래, 그것은 순진 알고리즘의 일종이지만, 당신의 목적에 너무 느려서는 안됩니다. 실제로 Udacity 101 클래스에서이 아이디어를 얻었는데, 비슷한 접근 방식을 사용하여 HTML 페이지에서 링크를 파싱했습니다. – Gohn67

0

이 작업은 수행 할 수 있지만 문자열을 여러 번 통과해야합니다.

$string = 'putting some stuff before the quote 
[quote][b]Logan said[/b][br]testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote] 

yep 

http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 

adding a quote 

[quote][b]Logan said[/b][br]This is the start of the second quote http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote] 

[i]04/07/12 20:18:07: Edited by Logan(2)[/i]putting some stuff before the quote 

[quote][b]Logan said[/b][br]testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote] 

yep 

http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 

adding a quote'; 

//get rid of whitespace 
$string = preg_replace('%\s\s?%', " ",$string); 
//break the string on a common element 
$pieces = preg_split('%\[%',$string); 
//now discard the elements that are tags 
foreach($pieces as $key=>$value): 
    $value = trim($value); 
    if(strrpos($value,"]") == (strlen($value) -1)): 
     unset($pieces[$key]); 
    endif; 
endforeach; 
print_r($pieces); 
//and finally strip out the tag fragments 
foreach($pieces as $key=>$value): 
    $pieces[$key] = preg_replace('%.*]%',"",$value); 
endforeach; 

결과는 다음과 같습니다 배열 :

Array 
(
    [0] => putting some stuff before the quote 
    [2] => Logan said 
    [4] => testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 
    [6] => did it work? 
    [9] => 04/04/12 23:48:46: Edited by Logan(2) 
    [13] => 04/04/12 23:55:44: Edited by Logan(2) 
    [15] => yep http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA adding a quote 
    [17] => Logan said 
    [19] => This is the start of the second quote http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 
    [21] => did it work? 
    [24] => 04/04/12 23:48:46: Edited by Logan(2) 
    [28] => 04/04/12 23:55:44: Edited by Logan(2) 
    [31] => 04/07/12 20:18:07: Edited by Logan(2) 
    [32] => putting some stuff before the quote 
    [34] => Logan said 
    [36] => testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 
    [38] => did it work? 
    [41] => 04/04/12 23:48:46: Edited by Logan(2) 
    [45] => 04/04/12 23:55:44: Edited by Logan(2) 
    [47] => yep http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA adding a quote 
)