Skip to content

Instantly share code, notes, and snippets.

@maynagashev
Last active October 31, 2016 08:51
Show Gist options
  • Save maynagashev/d883ba6a332816c82f800a6b00d7a5e9 to your computer and use it in GitHub Desktop.
Save maynagashev/d883ba6a332816c82f800a6b00d7a5e9 to your computer and use it in GitHub Desktop.
Prepares keywords for photo stock web sites: Shutterstock.com, iStockPhoto.com, Fotolia.com etc. Simple PHP parser script.
<?php
/*
From a plain text/description of photo, makes list of keywords, separated with comma, filtering articles and prepositions.
Example:
INPUT: Portrait of a happy bald man photographer with professional digital camera on his neck, he throws tons
of leaves falling in the air in autumn park, enjoying the sunshine weather, glad and satisfied.
OUTPUT: portrait, happy, bald, man, photographer, professional, digital, camera, neck, throws, tons, leaves, falling, air, autumn, park, enjoying, sunshine, weather, glad, satisfied
*/
$exclude = array(
'in', 'a', 'or', 'the', 'in', 'with', 'an', 'at', 'on', 'from', 'and', 'to', 'of', 'is', 'her', 'his', 'he', 'she', 'it'
);
$t = '';
$res = array();
if (isset($_POST['text'])) {
$t = $_POST['text'];
$ar = explode(" ", $t);
foreach ($ar as $w) {
$add = 0;
$w = strtolower($w);
$w = preg_replace("#[^a-z]#si", "", $w);
if ($w and !in_array($w, $exclude)) {
$add = 1;
}
if ($add) {
$res[] = $w;
}
}
echo count($res)."<pre>";print_r($res);
$t = implode(", ", $res);
}
echo "<pre>{$t}</pre>";
?>
<title>Keywords</title>
<p>
<form action="keys.php" method="post">
<textarea name="text" rows="10" cols="100"></textarea>
<input type="submit">
</form>
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment