SM9 SM9 - 2 months ago 5x
PHP Question

Regex to match previous names

I have been having some trouble writing regex to match previous names on this page:

To be clear, I want in an array the following:

  • TripleThreat

  • [FD] TripleThreat.blyat

  • 9

and so on..

I have already tried writing the Regex but it was a disaster (Something I struggle with)

Here's what I wrote:

$page = file_get_contents(sprintf(""));

preg_match_all("/<span class=\"historyDash\">-<\/span>((.|\n)*)<\/div>/", $page, $matches);

foreach($matches[0] as $match) {
echo($match . "<br/>");

Any help is much appreciated :)


You can try the following regex (the match is in the first capturing group):

"/<span class=\"historyDash\">-<\/span>\s*((?:[^\<]|\n)*?)\s*<\/div>/"

See it on Regex101.

The changes I made: trimmed whitespace before and after with the \s*, changed the . to [^\<] to choose only the ones that aren't tag (i.e., the correct text).