-1

Good day.

I want to file_get_content() to load a webpage and use strip_tags() to get the string: Category:Apple.

<div class="category">
    <span style="font-size:11px; font-weight:bold;">Category:</span>
           <a href="/listing/A/new/yp/search.do?applicationInd=A" 
                class="category">Apple
           </a>
</div>

eg.

$text = '<div class="category">
         <span style="font-size:11px; fontweight:bold;">
         Category:</span><a href="/listing/A/new/yp/search.do?applicationInd=A"
         class="category">Apple</a></div>';

         echo strip_tags($text); //Category:Apple

What php statement do I need to do to pass variable to $text for that ... ?

Use preg_match?

Ripa Saha
  • 2,532
  • 6
  • 27
  • 51
user1934737
  • 107
  • 1
  • 2
  • 10

1 Answers1

-1

Try this

preg_match('|<a href="([^"]*?)" class="category">([^>]*?)<\/a>|smi', $text, $matches);
$cat = "Category:" . $matches[2];
echo $cat;
  • I would feel better if you allowed for white space before the 'class' and arbitrary text before the closing '>'. If you really want to be careful, handle white space around the '=' as well, and single, as well as double quotes. – Carl Jun 18 '13 at 06:21
  • 1
    Don't encourage bad practices! http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not Always use a parser kids! – Bailey Parker Jun 18 '13 at 06:23
  • 1
    @Carl I would feel better if regex wasn't used in this case *at all*! – Bailey Parker Jun 18 '13 at 06:23