I’m trying to capture the text “Capture This” in $string below.
$string = "</th><td>Capture This</td>";
$pattern = "/<\/th>\r.*<td>(.*)<\/td>$/";
preg_match ($pattern, $string, $matches);
echo($matches);
However, that just returns “Array”. I also tried printing $matches using print_r, but that gave me “Array ( )”.
This pattern will only come up once, so I just need it to match one time. Can somebody please tell me what I’m doing wrong?
The problem is that you require a CR character
\r. Also you should make the search lazy inside the capturing group and useprint_rto output the array. Like this:You can see it in action here: http://codepad.viper-7.com/djRJ0e
Note that it’s recommended to parse html with a proper html parser rather than using regex.