0

Using preg_match function, I want to find the number 30899 from the following html string.

I've used the following line of code:

preg_match("/<a ?.* onclick=\"cms4i.models.dealersearch.view.navigate\('DETAIL', '(.*)'\);\" class=\"partner_name link\">Zentrum GmbH Standort<\/a>/i", $data, $matches);

It doesn't return any matching string, though $data has 30899 here.

$data variable is as follows:

<li class="partner_teaser clearfix">
  <div class="partner_gutter">
    <a href="#" onclick="cms4i.models.dealersearch.view.navigate('DETAIL', '30899');" class="counter link">01</a>
    <i class="icon_car" title="Partner (Verkauf)">Partner (Verkauf)</i>
    <i class="icon_spanner" title="Partner (Service)">Partner (Service)</i>
  </div>
  <div class="partner_summary">
  <a href="#" onclick="cms4i.models.dealersearch.view.navigate('DETAIL', '30899');" class="partner_name link">Zentrum GmbH Standort</a>
  <div>Franklinstraße 24</div><div>10587 City</div>
  <div>5,4 km</div>
  <a href="#" onclick="cms4i.models.dealersearch.view.navigate('DETAIL', '30899');" class="link link_a1">Auf Karte anzeigen</a>
  </div>
</li>

Though I solved this kind of problems earlier, I can't find the solution how to find these numbers from this string. I believe an expert can find its solution quickly.

3
  • 1
    Well, first you need to escape your periods, otherwise, isn't it working for you? Seems to work here. Or is that you want to get all the 30899? Commented May 30, 2013 at 11:14
  • Yes, your are right. Now, I've used \ to escape the periods. But, it doesn't return any matching string. Commented May 30, 2013 at 11:20
  • I guess it's the function then. Perhaps you might want to use the g flag in addition to the i flag you already used. Or preg_match_all like enenen suggested. Commented May 30, 2013 at 11:22

2 Answers 2

2

As I see you are searching for number which is parameter in the navigate method. So, you can try with:

preg_match_all('/\.navigate\([a-zA-Z\,\s\']+([0-9]{3,})\'\);" class="partner_name link"\>Zentrum GmbH Standort\<\/a\>/', $html, $matches);

print_r($matches[1]);

Output:

Array
(
    [0] => 30899
    [1] => 30899
    [2] => 30899
)

Then you can exclude repeated numbers if it's possible to have more than one unique number. If not, extract only one number, not all.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Yes, it returns all of different numbers displayed on those <li> tag. How can I find the number near to "Zentrum GmbH Standort". Please note the full html page contains hundreds of different number. I want to find specific number using 'class="partner_name link">Zentrum GmbH Standort</a>'.
I edited my answer. But if you want the number used in/near a html element then probably it would be better to use DOMDocument instead of regular expressions.
0

What about?

preg_match('/<a (.*) onclick=/', $data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.