0

from the phrase:

 <div class="latestf"> <a href="http://www.x.ro/anamaria/"
 rel="nofollow"

I want to extract anamaria. How to do that with preg_match_all ?

I tried:

preg_match_all("'<div class=\"latestf\">
<a href=\"http://www.x.ro/(.*?)\" rel=\"nofollow\"'si", $source, $match);

but it didn`t work...

Thank you in advance !

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • 1
    **What does "doesn't work" mean?** "Doesn't work" is an inadequate description for us to understand the problem. What happened when you tried it? Did you get an error message? Did you get incorrect results? Did you get *no* results? If the results were incorrect, what made them incorrect? What were you expecting instead? Did you get *any* correct results? If so, what were they? Don't make us guess. – Andy Lester Sep 09 '13 at 12:17
  • edited now with code quotes – user2761358 Sep 09 '13 at 12:17
  • it just returns no result..like i`ve missed something – user2761358 Sep 09 '13 at 12:19
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – faino Sep 09 '13 at 12:20
  • 1
    Is the newline in the regex intentional? Also, you should escape your periods: `.` -> `\.` – Jerry Sep 09 '13 at 12:21

3 Answers3

1

Try this:

$source = '<div class="latestf"> <a href="http://www.x.ro/anamaria/" rel="nofollow"';


preg_match_all('#<div\s*class="latestf">\s*<a\s*href="http://www\.x\.ro/(.*?)/?"\s*rel="nofollow"#i', $source, $match);

print_r($match);

Array
(
    [0] => Array
        (
            [0] => <div class="latestf"> <a href="http://www.x.ro/anamaria/" rel="nofollow"
        )

    [1] => Array
        (
            [0] => anamaria
        )

)
1

Don't try to parse HTML with regex. Use a DOM parser instead:

$html = '<div class="latestf"> <a href="http://www.x.ro/anamaria/"
 rel="nofollow"';

$dom = new DOMDocument;
@$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $node)
{
    $link = $node->getAttribute("href");
}

$parsed = parse_url($link);

echo substr($parsed['path'], 1, -1);

Output:

anamaria

Demo!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • Don't use `@`. If there's an error, you should know something is wrong. Blundering along, hoping for the best can be harmful, a notice isn't (not really) – Elias Van Ootegem Sep 09 '13 at 12:41
  • @EliasVanOotegem: I agree that error messages shouldn't be suppressed, but in this case, `@` sounds like a good idea since the HTML isn't properly formatted. – Amal Murali Sep 09 '13 at 12:46
0

/ should be escaped like this \/

<?php

  $source = '<div class="latestf"> <a href="http://www.x.ro/anamaria/" rel="nofollow"';

  preg_match_all('/<div class="latestf"> <a href="http:\/\/www.x.ro\/(.*?)\/" rel="nofollow"/', $source, $match);

  var_dump($match);exit;
wuya
  • 1
  • 1