Skip to content

Instantly share code, notes, and snippets.

View stevelacey's full-sized avatar
🚀
Shipping

Steve Lacey stevelacey

🚀
Shipping
View GitHub Profile
@stevelacey
stevelacey / PHP: Simple HTML Table Scrape
Created October 29, 2010 21:46
PHP: Simple HTML Table Scrape
<?php
$doc = new DOMDocument();
// It's rare you'll have valid XHTML, suppress any errors- it'll do its best.
@$doc->loadhtml($string);
$xpath = new DOMXPath($doc);
// Modify the XPath query to match the content
@stevelacey
stevelacey / Twitter user feed + html content.php
Created December 28, 2010 20:29
Symfony task extract for parsing Twitter user atom feed and extra data from Twitter status API.
<?php
$this->logSection($this->namespace, 'Getting latest tweets for @'.$user->getUsername());
$web = new sfWebBrowser();
$atom = $web->get('http://search.twitter.com/search.atom?q=from:'.$user->getUsername().'&rpp=5');
try {
if(!$atom->responseIsError()) {
$feed = new SimpleXMLElement($atom->getResponseText());
@stevelacey
stevelacey / FrontendUrlHelper.php
Created January 15, 2011 23:56
Allows you to link to routes in your frontend application from the backend via sfProjectConfiguration::getActive()->generateFrontendUrl($routeName, $params); and the usual helper functions prepended with _frontend. The FrontendUrlHelper functions are full
<?php
function link_to_frontend() {
// for BC with 1.1
$arguments = func_get_args();
if (empty($arguments[1]) || is_array($arguments[1]) || '@' == substr($arguments[1], 0, 1) || false !== strpos($arguments[1], '/')) {
return call_user_func_array('link_to_frontend1', $arguments);
} else {
if (!array_key_exists(2, $arguments)) {
$arguments[2] = array();
@stevelacey
stevelacey / PHP Tweet Sentiments
Created January 24, 2011 16:45
sentiment.php
<?php
$response = file_get_contents('http://data.tweetsentiments.com:8080/api/analyze.json?'.http_build_query(array('q' => $_GET['tweet'])));
$json = json_decode($response);
echo $json->sentiment->name,"\n",$json->sentiment->value,"\n";
@stevelacey
stevelacey / _form_fieldset.php
Created March 13, 2011 18:32
Add an auto-detected partial for form generator fieldset grouping help text. Goes in "data/generator/sfDoctrineModule/admin/template/templates/". Lets you describe a group of fields in a Symfony form, based on adding a partial to the module under the foll
<fieldset id="sf_fieldset_[?php echo preg_replace('/[^a-z0-9_]/', '_', strtolower($fieldset)) ?]">
[?php if ('NONE' != $fieldset): ?]
<h2>[?php echo __($fieldset, array(), '<?php echo $this->getI18nCatalogue() ?>') ?]</h2>
[?php endif; ?]
[?php if (is_readable('<?php echo sfConfig::get('sf_app_module_dir').'/'.$this->getModuleName() ?>/templates/_'.preg_replace('/[^a-z0-9_]/', '_', strtolower($fieldset)).'_help.php')): ?]
[?php include_partial('<?php echo $this->getModuleName() ?>/'.preg_replace('/[^a-z0-9_]/', '_', strtolower($fieldset)).'_help') ?]
[?php endif ?]
...
[Constructor(in DOMString url, in optional DOMString protocols)]
[Constructor(in DOMString url, in optional DOMString[] protocols)]
interface WebSocket {
readonly attribute DOMString url;
// ready state
const unsigned short CONNECTING = 0;
const unsigned short OPEN = 1;
const unsigned short CLOSING = 2;
const unsigned short CLOSED = 3;
@stevelacey
stevelacey / counties.php
Created April 18, 2012 08:45
UK Counties Array
<?php
// Source: http://www.carronmedia.com/uk-postal-counties-list
array(
'England' => array(
'Avon',
'Bedfordshire',
'Berkshire',
'Buckinghamshire',
@stevelacey
stevelacey / sfWidgetFormCountyChoice.class.php
Created April 18, 2012 08:59
sfWidgetFormCountyChoice
<?php
class sfWidgetFormCountyChoice extends sfWidgetFormChoice {
/**
* Constructor.
*
* Available options:
*
* * choices: An array of possible choices (defaults to counties)
* * multiple: true if the select tag must allow multiple selections
@stevelacey
stevelacey / UrlGenerator.php
Last active October 7, 2015 21:18
Symfony2: use object to set route parameters
<?php
namespace Acme\BaseBundle\Routing\Generator;
use Symfony\Component\Routing\Generator\UrlGenerator as BaseUrlGenerator;
use Doctrine\Common\Util\Inflector;
/**
* UrlGenerator generates URL based on a set of routes.
*
@stevelacey
stevelacey / ChunkExtension.php
Created August 2, 2012 09:16
Twig Chunk Extension
<?php
namespace Acme\DemoBundle\Twig;
use Twig_Extension;
use Twig_Filter_Method;
class ChunkExtension extends \Twig_Extension
{
public function getFilters()