0

I have a problem with a regex pattern. It returns two arrays as a result... Here is my code:

$code = preg_match_all("/\< style\>(.*?)\<\/style\>/",$code,$matches);
var_dump($matches);

As test I set:

$code = ">< xxxxx> try blah fooo blah   < /xxxxx> idfidf oh < x>< /x> < style> blah blah blah style1 < /style>< style>blah blah style 2 x< /style>

It returns 2 arrays, I mean

$matches = array
    0 => array
        0 => string '< style> blah blah blah style1 < /style>' (length=38)
        1 => string '< style>blah blah style 2 x< /style>' (length=34)
    1 => array
        0 => string ' blah blah blah style1 ' (length=23)
        1 => string 'blah blah style 2 x' (length=19)

The matches I want are in the second array. I put space between the tags, because the editor not showing the HTML tags.

3 Answers 3

1

Did you tried this:

echo $matches[0];
echo $matches[1]; // and so on, depend in the number of matches.
Sign up to request clarification or add additional context in comments.

Comments

1

Following code is working for me:

$code = "<xxxxx> try blah fooo blah </xxxxx> idfidf oh <x></x> <style> blah blah blah style1 </style><style>blah blah style 2 x</style>";
$code = preg_match_all("~<style>(.*?)</style>~si", $code, $matches);
var_dump($matches[1]);
  • Modifier s is for DOT_ALL (including newline)
  • Modifier i is for ignore case match

OUTPUT

array(2) {
  [0]=>
  string(23) " blah blah blah style1 "
  [1]=>
  string(19) "blah blah style 2 x"
}

However just to let you know that parsing HTML from regex is not a very good idea, you would be better off using many HTML parsers available for php.

Comments

0

How about using Xpath? http://nl.php.net/manual/en/class.domxpath.php

Works for me (most of the times, that is ;) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.