Get metadata snippet of 4 chars

Hello,

I read the documentation thoroughly but am still scratching my head about this one.
I have multiple entries in each metadata field and am trying to organize them by editing /themes/default/items/show.php

So far, I’ve gotten it to return multiple values with one on each line, but am having trouble when I try to ask for a particular subset of a string.I would like only the first four characters (numbers) shown, but when I use the following code, it will only return meaningful values if I set “snippet” to over 15 chars. Is there any way to have it return only the first four?

<?php echo metadata('item',array('Dublin Core', 'Relation'), array('delimiter' => '</br> ', 'all' => null, 'snippet' => 12)); ?></div>

Thanks for your help!

-B

What do you mean when you say you don’t get “meaningful” results with lower numbers? What do you get?

Sorry, I should have been clearer. I get three dots “…”

I’ve uploaded a screenshot here. I’ve tried this both in a table and outside of a table with no success.

With:
<?php echo metadata('item',array('Dublin Core', 'Relation'), array('delimiter' => '</br> ', 'all' => null, 'snippet' => 4); ?>
I get this output

The trick here is that snippet isn’t intended to work like a regular substring that just cuts the string at the length you select. Instead, it only wants to break at word boundaries. So, if you select a snippet length that’s shorter than the first word in the text, you get nothing back.

Obviously that’s not so good for your situation. I don’t think it’s solvable with metadata alone, at least as it’s currently written.

$relations = metadata('item', array('Dublin Core', 'Relation'), array('all' => true));
foreach ($relations as &$relation) {
    $relation = substr($relation, 0, 4);
}
echo implode('<br>', $relations);

It’d probably be useful for us to provide a callback functionality for metadata so breaking out into a loop like this isn’t necessary, but for now this is your best bet.

Great! Thanks for the help!!

Just in case anyone is dealing with this in future, the solution above kept throwing invalid arguments in the “foreach” loop. I realized that this is because I have “search by metadata” turned on for the DC:Relation metadatum (and need it to stay on elsewhere in the DB). I re-opened my CSV file, undid the previous import, and added in a column with the same values as “relation” under a new variable name (PrintedEdYear - which was not enabled in the Search by Metadata configuration).

I was still having problems with the “foreach” loop you suggested, so I ended up doing this and it worked like a charm:

  
<?php $relations = metadata('item',array('Item Type Metadata', 'PrintedEdYear'), array('all' => true));
	   foreach ($relations as $relstr) {
		   echo substr($relstr, 0, 4), "
"; } ?>

I’ve included a screenshot with the final output (visible in far right column).

Hmm…that sounds like there might be a bug in SearchByMetadata, then. I’ll look into it.

UPDATE: Couldn’t reproduce the invalid argument issue, but it did break because the plugin adds the link, which screws up your substring() call