class HTMLEncoder_VariableModifier extends VariableModifier { public function modify($value) { return htmlentities($value); } } class RemoveURLProtocol_VariableModifier extends VariableModifier { public function modify($value) { return preg_replace('|^http://|', '', $value); } } class UrlSpaceEncoder_VariableModifier extends VariableModifier { public function modify($value) { return preg_replace('/\\+/', '%20', urlencode($value)); } } class UrlUnderscoreEncoder_VariableModifier extends VariableModifier { public function modify($value) { return preg_replace('/\\+/', '_', urlencode($value)); } } // static members can't be populated with objects so we need to set them separately SharingButtons::$VariableModifiers = array('urlencoded' => new UrlEncoder_VariableModifier(), 'htmlencoded' => new HTMLEncoder_VariableModifier(), 'spaceencoded' => new UrlSpaceEncoder_VariableModifier(), 'underscoreencoded' => new UrlUnderscoreEncoder_VariableModifier(), 'noprotocol' => new RemoveURLProtocol_VariableModifier());
<?php require_once __DIR__ . '/SharingButtons.php'; $template = 'http://digg.com/submit?title={$title}&bodytext={$description}&url={$url.raw}'; $variables = array('url' => 'http://www.sharingbuttons.org/', 'title' => 'Sharing Buttons', 'description' => 'SharingButtons.org is a catalog of sharing services and their buttons'); $expect = 'http://digg.com/submit?title=Sharing+Buttons&bodytext=SharingButtons.org+is+a+catalog+of+sharing+services+and+their+buttons&url=http://www.sharingbuttons.org/'; $result = SharingButtons::substituteVariablesInString($template, $variables, 'urlencoded'); echo ($expect == $result ? 'success' : 'fail') . "\n"; // testing for modifiers $template = '{$a} {$b.urlencoded} {$c.spaceencoded}'; $expect = '{$a} y+y+y z%20z%20z'; $variables = array('b' => 'y y y', 'c' => 'z z z'); $result = SharingButtons::substituteVariablesInString($template, $variables); echo ($expect == $result ? 'success' : 'fail') . "\n"; $expect = 'x x x {$b.urlencoded} z%20z%20z'; $variables = array('a' => 'x x x', 'c' => 'z z z'); $result = SharingButtons::substituteVariablesInString($template, $variables); echo ($expect == $result ? 'success' : 'fail') . "\n"; $expect = 'x x x y+y+y z%20z%20z'; $variables = array('a' => 'x x x', 'b' => 'y y y', 'c' => 'z z z'); $result = SharingButtons::substituteVariablesInString($template, $variables); echo ($expect == $result ? 'success' : 'fail') . "\n";
<?php require_once __DIR__ . "/php/SharingButtons.php"; $lines = array_map(function ($line) { return str_getcsv($line); }, file('php://stdin')); $sample_data = array('title' => 'Sharing Buttons', 'description' => 'Code libraries to work with sharing buttons', 'url' => 'https://github.com/sergeychernyshev/Sharing-Buttons', 'journalname' => 'sergeyche', 'tags' => 'share', 'social media', 'buttons', 'query' => 'Sharing Buttons'); $out = array(); foreach ($lines as $line) { $icon = $line[0]; $title = $line[1]; $description = $line[2]; $url = $line[3]; $sample = SharingButtons::substituteVariablesInString($url, $sample_data, 'urlencoded'); echo "## {$title}\n\n{$description}\n\n![{$title}](https://raw.githubusercontent.com/sergeychernyshev/Sharing-Buttons/master{$icon}) <a href='{$sample}' target='blank'>{$title}</a>\n\nURL: `{$url}`\n\n"; }