Skip to content

Instantly share code, notes, and snippets.

View alxfv's full-sized avatar

Alexander Fedorov alxfv

View GitHub Profile
@alxfv
alxfv / gist:6219436
Last active December 21, 2015 00:19
Symfony ajax form validation
<?php
* @Route("/{id}/edit", name="user_edit")
* @Template()
*/
public function editAction($id)
{
/** @var $dm \Doctrine\ODM\MongoDB\DocumentManager */
$dm = $this->get('doctrine_mongodb')->getManager();
{
"name": "symfony/framework-standard-edition",
"description": "The \"Symfony Standard Edition\" distribution",
"autoload": {
"psr-0": { "": "src/" }
},
"require": {
"php": ">=5.3.3",
"symfony/symfony": "2.2.*",
"twig/extensions": "1.0.*",
<?php
// hook_nodeapi
if ($op == 'presave') {
foreach ($node->field_reply as $key => $field) {
if (strpos($field['value'], '<!-- time:') === FALSE) {
$node->field_reply[$key]['value'] = $field['value'] . '<br /><!-- time: ' . date() . ' -->';
}
@alxfv
alxfv / gist:5458446
Created April 25, 2013 08:57
Add attribute rel nofollow
<?php
$str = '<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<a href="http://km.ru">test</a>
@alxfv
alxfv / gist:5335130
Last active December 15, 2015 22:48
<?php
$ttl = 5 * 3600;
$refresh_time_limit = strtotime('2013-04-07 13:00'); // variable_get
$limit = 10; // variable_get
$max_limit = 450; // variable_get
$now = time(); // 12:15
$is_hour = ($now - $refresh_time_limit) > 3600; // true
<iframe id="myIframe" src="" width="400" height="100" frameborder="0"></iframe>
<script type="text/javascript">
var url = 'http://lenta.ru';
document.getElementById('myIframe').src = url + '?p=' + Math.floor(Math.random() * 1000);
</script>
<?php
db_set_active('knigi');
$result = db_query('SELECT nid FROM node where uid <> 0');
$nids = array();
while ($row = db_fetch_array($result)) {
$nids[] = $row['nid'];
}
@alxfv
alxfv / gist:4619894
Last active December 11, 2015 15:18
def combine_anagrams(words)
output = {}
words.each do |word|
index = word.downcase.chars.sort.join
output[index].is_a?(Array) ? output[index] << word : output[index] = [word]
end
output.values
end
def rps_game_winner(game)
raise WrongNumberOfPlayersError unless game.length == 2
2.times do |i|
raise NoSuchStrategyError unless ['P', 'S', 'R'].include? game[i][1].upcase
end
first = game[0][1].upcase
second = game[1][1].upcase
if first == 'P'
@alxfv
alxfv / gist:4604329
Last active December 11, 2015 12:58
hw1-1
def palindrome?(str)
str.downcase.gsub(/\W/, '') == str.downcase.gsub(/\W/, '').reverse
end
def count_words(str)
words = {}
str.downcase.split(/\W+/).each do |word|
words[word] = words.include?(word) ? words[word] + 1 : words[word] = 1
end
words