3

I need to take the output of $one and make an array, however, the regex is not right because all I get back is an array with one key and one value; basically, the entire string. If there are 10 paragraph tags in the string then I should get 10 values in the array.

What is wrong with my regex?

Ideally, what I would like to have in the output array are two arrays, one with the paragraph tags and another with just the text between them.

$one = preg_replace( '/<p>/i', '<p class="test">', $str ); // This gives me what I need
print_r(explode( '/<p class="test">/iU', $one )); // This does not
NaN
  • 1,286
  • 2
  • 16
  • 29
  • 2
    I know i shouldn't but... http://stackoverflow.com/a/1732454/496223 – dynamic Dec 04 '12 at 00:41
  • 1
    @yes123, thanks for the link. I thought regex could parse anything.. Would you have a suggestion how I get the output of $one into an array? – NaN Dec 04 '12 at 00:46
  • afaik, `explode` cannot split according to a regex, but `preg_split` might help. – didierc Dec 04 '12 at 00:47
  • Yea, I did palm face too when I discovered regex can't do everything – dynamic Dec 04 '12 at 00:48
  • note that because of the nested nature of html, regexps might not be the best way to handle it – didierc Dec 04 '12 at 00:48
  • maybe you need an [html parser](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) compatible with xpath queries. – didierc Dec 04 '12 at 00:53
  • Just to clarify, there will be no nested html to parse. I am only ever going to throw paragraph tags at it. – NaN Dec 04 '12 at 01:00

1 Answers1

0

The problem is simple. Explode does not use regex. This should work for you.

print_r(explode('<p class="test">', $one ));

EDIT: This ought to do what you want.

$pattern = '/(?P<open><p class="test">)'
          . '(?P<content>.*)'
          . '(?P<close><\/p>)/i';
preg_match_all($pattern, $one, $matches);
print_r($matches);

EDIT: Simplified version without the match tags:

$pattern = '/(<p class="test">)(.*)(<\/p>)/i';
preg_match_all($pattern, $one, $matches);
print_r($matches);
Steven Moseley
  • 15,871
  • 4
  • 39
  • 50