0

For one reason or another this (messy thing) doesn't work.. it's messy because I changed so much to find out what is wrong..

Problem: preg_match("/\"h4(.*?)/h4/s", $theMAGICresult, $output_game);

I know the match works.. (normaly); I did check on http://www.phpliveregex.com/. I did it in a foreach loop, while loop.. I try preg_match_all.. I supply a correct pattern, a correct string for subject and I put it in a correct array and fetch it but it is empty.. after 2 hours of finding an answer I give up.. can you help me ??

preg_match_all("/<li class=\"completed\">(.*?)<\/li>/s", $all, $output);
/* $all contains something like        xx <li class="completed"> xxxx"h4GOLDFISH/h4xxxxxx</li>xxxxxxxxxxxx */
$arrayLoop = array(); //makes sure it's an array
$arrayLoop = $output[0]; // makes $output[0] the default array

//$arrayLoop[0] is now <li class="completed"> xxxx"h4GOLDFISH/h4xxxxxx</li>
$i = 1; //while loop because, had first foreach loop etc..
while($i < count($arrayLoop)) {
$theMAGICresult = $arrayLoop[$i]; // to be sure $arrayLoop[$i] is valid

//theMAGICresult is now : <li class="completed">xx"h4GOLDFISH/h4xxxxxx</li>
preg_match("/\"h4(.*?)\/h4/s", $theMAGICresult, $output_game);

echo $output_game[1]; //is nothing while is should have GOLDFISH

    $i++;
}
print_r($output_game); //empty array
brandon Whe
  • 206
  • 1
  • 14
  • If I had to guess, I'd say you're trying to parse some HTML. You tried with a regex but got stuck and now you're trying to force your perceived solution. This is known as [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). There's also an excellent explanation on [why you can't parse HTML with regular expressions](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). Have you tried PHP's [DOMDocument](http://php.net/manual/en/class.domdocument.php)? If not, you should. – N.B. Jul 20 '15 at 16:52
  • [Works for me](http://3v4l.org/3BmlK). – Siguza Jul 20 '15 at 16:56
  • @N.B. thanks but i'm handling the html as a string... //string(722) i even removed the '< & >' from the H4 to make sure it is a string.. –  Jul 20 '15 at 16:56
  • @Siguza Thanks.. now i know it's not the html thing.. i'm looking the wrong way i guess.. –  Jul 20 '15 at 17:00

1 Answers1

1

try with:

preg_match("/\\"h4(.*?)\/h4/s", $input_line, $output_array);

Fixed example:

$output_game = [];
$theMAGICresult = '<li class="completed">xx"h4GOLDFISH/h4xxxxxx</li>';
preg_match('/\"h4(.*?)\/h4/s', $theMAGICresult, $output_game);
var_dump($output_game);