We help IT Professionals succeed at work.

Filter content function

Hi

The attached function should return the category name contained in this HTML string:
http://www.frostwave.com/cat.html

The result that should return on this example is "Base de Datos"

The name is contained here:
<a rel="category tag" title="Ver todas las entradas en Base de Datos" href="http://www.mysite.com/category/base-de-datos">Base de Datos</a>

Open in new window


I do appreciate any help.
Thanks
<?php 
error_reporting(E_ALL);
echo "<pre>";


// TEST DATA FROM THE POST AT EE
$url = 'http://www.frostwave.com/cat.html';



function filter_category($content){
		$category = '';

// code here 
	
		return $category;
	}

// SHOW THE WORK PRODUCT
echo filter_category($url);

Open in new window

Comment
Watch Question

Marco GasiFreelancer
Top Expert 2010

Commented:
Try this:

function filter_category($content){
            $category = '';
                if (preg_match('%(?<=>).*(?=</a>)%ix', $subject, $matches[0])) {
                          $category = $matches[0];      
              }
            return $category;
      }

Author

Commented:
I get Undefined variable: subject in ... who is $subject ?

Commented:
$subject = $content;

Author

Commented:
Then it returns empty.

  if (preg_match('%(?<=>).*(?=</a>)%ix', $content, $matches[0])) {
                          $category = $matches[0]; 

Open in new window


I tried that but it returned empty. Any idea?

Commented:
Try this

<?php 
error_reporting(E_ALL);
echo "<pre>";


// TEST DATA FROM THE POST AT EE
$url = 'http://www.frostwave.com/cat.html';



function filter_category($content){
	$category = '';

	$html = file_get_contents($content);
	$preg = '{<a [^>]*rel="category tag"[^>]*>(.*)</a>}U';

// code here
	if (preg_match($preg, $html, $match)) {
		$category = $match[1];
	}
        
	return $category;
}

// SHOW THE WORK PRODUCT
echo filter_category($url);

?>

Open in new window

Author

Commented:
StingRaY, that worked great. ( result: "Base de Datos" ) perfect!

StingRaY let me ask you one small thing please:

I would like assign one ID for every category name and have as result the ID instead of "Base de Datos".

Sothing like;

$categories = array(1 => 'Base de Datos', 3 => 'Economy');

then have as result: 1 instead of "Base de Datos"

Is possible to do this here?
Commented:
Sure.

<?php 
error_reporting(E_ALL);
echo "<pre>";


// TEST DATA FROM THE POST AT EE
$url = 'http://www.frostwave.com/cat.html';



function filter_category($content){
	$category = '';

	// Indexed category list
	$categories = array(1 => 'Base de Datos', 3 => 'Economy');
	// Flip the list to create inverted list
	$inverted_cat = array_flip($categories);

	$html = file_get_contents($content);
	$preg = '{<a [^>]*rel="category tag"[^>]*>(.*)</a>}U';

// code here
	if (preg_match($preg, $html, $match)) {
		$category = $match[1];
	}

	if (isset($inverted_cat[$category])) {
		return $inverted_cat[$category];
	}

	return -1; // not found;
}

// SHOW THE WORK PRODUCT
echo filter_category($url);

?>

Open in new window


It would be faster if you can build the inverted list to reduce the cost of execution.
The inverted list will be something like this.

$inverted_cat = array('Base de Datos' => 1, 'Economy' => 3);

Author

Commented:
Awesome, thank you!