Logan Best
Logan Best

Reputation: 501

preg_match_all getting weird

I have some text that's wrapped in [quote][/quote] and I'm trying to match all text before those tags, everything in between those tags, and everything after those tags. The catch is that there may be multiple occurrences of them, but not within each other.

The reason for me doing this is because I want to run a filter on all the text outside of those tags whether there's multiple occurrences or not.

This is what I'm starting to work with:

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

Here's the output:

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]
    )

)

As you can see it's not getting the desired output. Any help would be appreciated.

Upvotes: 0

Views: 92

Answers (2)

Odyssey
Odyssey

Reputation: 133

It can be done but you will need to make multiple passes over the string.

$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;

The result is an array that looks like this:

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
)

Upvotes: 0

Gohn67
Gohn67

Reputation: 10638

I haven't tried this, but you only want the stuff before [quote] and after [/quote], you could do a strpos for the first occurrence of the opening quote tag. Now you know everything before is not quoted.

Next you can use strpos starting from the index of the first matching quote tag to find the closing quote tag. You can discard this stuff.

Now do another strpos for the next quote block using a starting position of the closing quote tag you just found. You can repeat this until you get to the end.

Upvotes: 1

Related Questions