user3089871 user3089871 - 7 months ago 13
PHP Question

preg_match Regex wont find matches (but it works on regex101)

Here is my regex101 example which works : https://regex101.com/r/fE6rO9/2 (you have to wait a few secondes because it's big )

here is my PHP

$content = htmlentities($contentCode);
/* correct echo when i copy paste it into regex101 */
echo $content;

// copy past from regex101
$re = "/<\\/form><table class=\"forumline\" width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">(.*)<\\/table><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">/";

preg_match($re, $content, $matches);

var_dump($matches);


but when i run it, it echoes :

array (size=0)
empty


Any idea what the problem is ?

This is the regex taken appart :

"/<\\/form><table class=\"forumline\" width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">(.*)<\\/table><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">/";

Answer

I've tested your regex and it seems to work if you remove

$content = htmlentities($contentCode);

simply use:

$content = $contentCode;

Notes:

  1. Please make sure you read You can't parse [X]HTML with regex
  2. Some alternatives to regex are DOMDocument or simplehtmldom