Fast and Simple youtube embedding

You can embed as many videos as you want and it still be fast.

HTML:

  <div class="video_name">Test video</div>
   <div class="video" style="background-color:#ccc; width:420px; height:315px;">
      <a href="http://www.youtube.com/watch?v=VIDEO_CODE" class="youtube"></a>
  </div>

JS:

<script type="text/javascript">
    function getYoutubeID(url) {
        var id = url.match("[\\?&]v=([^&#]*)");
        id = id[1];
        return id;
    };
    $('a.youtube').each(function() {
        var id = getYoutubeID( this.href );
        this.id = id;
        var thumb_url = "http://img.youtube.com/vi/"+id+"/0.jpg";
        $('<img width="100%" src="'+thumb_url+'" />').appendTo($(this.parentNode));

    });

    $('.video').on('click', function(e) {
        var link = $(this).find('a')[0];
        var id = getYoutubeID( link.href );
        $(this).html('<iframe width="420" height="315" src="//www.youtube.com/embed/'+ id +'?autoplay=1" frameborder="0" allowfullscreen></iframe>').css('background', 'none');
    });
</script>
 

MODx truncate function

Создаем сниппет с именем phx:character_limit

<?php
if (strlen($output) > $options) {
  $output_cutted = substr($output, 0, $options);
  $last_space = strrpos($output_cutted, " ");
  $output = substr($output_cutted, 0, $last_space) . " ...";
        preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $output, $result);
    $openedtags = $result[1];

    #put all closed tags into an array
    preg_match_all('#</([a-z]+)>#iU', $output, $result);
    $closedtags = $result[1];
    $len_opened = count($openedtags);

    # all tags are closed
    if (count($closedtags) == $len_opened) {
        return $output;
    }
    $openedtags = array_reverse($openedtags);

    # close tags
    for ($i=0; $i < $len_opened; $i++) {
        if (!in_array($openedtags[$i], $closedtags)){
            $output .= '</'.$openedtags[$i].'>';
        } else {
            unset($closedtags[array_search($openedtags[$i], $closedtags)]);
        }
    }
  }
return $output;
?>

Пример вызова:

[+content:character_limit=`300`+]
 

The way to store config options in ruby without using ENV variables

# my_project/config.rb
module MyProject
  class Config
    attr_accessor :my_cool_option, :my_other_option
  end
end

# my_project.rb
module MyProject
  def self.config
    @config ||= Config.new
  end

  def self.configure
    yield(config)
  end
 

locales error after runing psql

To fix this really annoying bug:

vim /etc/environment
// Add to the end:
LC_ALL="en_US.utf-8"
 

karma start not running any js tests

We should start karma directly from test folder or from root with next code:

karma start test/karma.conf.js
 

php mail() script with attachment

<?php
    if ((isset($_POST['contact_phone']) && !empty($_POST['contact_phone'])) || (isset($_POST['contact_email']) && !empty($_POST['contact_email']))){
      $tmpName = $_FILES['attachment']['tmp_name'];
      $fileType = $_FILES['attachment']['type'];
      $fileName = $_FILES['attachment']['name'];

      $headers = "From: noreply@test.com";

      $message = '<h2>Получено новое сообщение. Данные:</h2><br><br><b>Имя: </b>'.$_POST['contact_name'].'<br><b>Телефон: </b>'.$_POST['contact_phone'].'<br><b>Email: </b>'.$_POST['contact_email']."<br><br><hr><p style='color: #ccc;'>Это письмо автоматическое и не требует ответа.</p><br>";

      if (file($tmpName)) {
        /* Reading file ('rb' = read binary)  */
        $file = fopen($tmpName,'rb');
        $data = fread($file,filesize($tmpName));
        fclose($file);

        /* a boundary string */
        $randomVal = md5(time());
        $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";

        /* Header for File Attachment */
        $headers .= "\nMIME-Version: 1.0\n";
        $headers .= "Content-Type: multipart/mixed;\n" ;
        $headers .= " boundary=\"{$mimeBoundary}\"";

        /* Multipart Boundary above message */
        $message = "This is a multi-part message in MIME format.\n\n" .
        "--{$mimeBoundary}\n" .
        "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
        $message . "\n\n";

        /* Encoding file data */
        $data = chunk_split(base64_encode($data));

        /* Adding attchment-file to message*/
        $message .= "--{$mimeBoundary}\n" .
        "Content-Type: {$fileType};\n" .
        " name=\"{$fileName}\"\n" .
        "Content-Transfer-Encoding: base64\n\n" .
        $data . "\n\n" .
        "--{$mimeBoundary}--\n";
      }

      mail('email@test.com', 'Новое сообщение из формы сайта', $message, $headers);
    }
  ?>
 

Find/replace with regexp in sublime text example

find         - unless (.*)(blank\?$)
replace      - if $1present?