0

I need to find out, whether in an array there is a specific HTML code. The array contains HTML codes and I need to get a number, that is included in a link. This would be what I am searching for (the number 10 ist the number I want): class = "active" href = "http://www.example.com/something-10

So I tried the following using preg_match:

if(preg_match('/class = "active" href = "http://www.example.com/something-(.*)/',$array["crawler"],$arr)) { print_r($arr,true); }

Unfortunately this will give me nothing as result. So I guess, something is wrong with my preg_match. I allready checked all the manuals, but I still dont get what I am doing wrong.

Could someone help me with this? Thank you!

phpheini

1 Answers1

2

Aside from advising you to not parse HTML using regular expressions, your particular regular expression needs different delimiters:

preg_match('~class = "active" href = "http://www\.example\.com/something-(\d+)~', ...)

Alternatively, you could have escaped the slashes within the regex, but that leads to LSS (leaning slash syndrome):

preg_match('/class = "active" href = "http:\/\/www\.example\.com\/something-(.*)/', ...)

And that's just ugly.

You should have gotten an error, if your error_reporting is turned on.

Community
  • 1
  • 1
Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
  • Thanks, the problem is that now everything after the number will be shown, I only need the number. So, now output is: 50.htm"> – phpheini Jan 28 '11 at 11:13
  • @phpheini Updated the answer; changed `.*` to `\d+` (`\d+` means "one digit or more"). – Linus Kleen Jan 28 '11 at 11:16