Skip to content

Instantly share code, notes, and snippets.

View lonalore's full-sized avatar

Sándor Juhász lonalore

View GitHub Profile
@lonalore
lonalore / RedirectWebformHandler.php
Last active November 21, 2021 23:58
Drupal 8 Webform Redirect Handler supports tokens
<?php
namespace Drupal\CUSTOM_MODULE\Plugin\WebformHandler;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Url;
use Drupal\webform\Plugin\WebformHandlerBase;
@lonalore
lonalore / br2nl.php
Created August 21, 2019 12:51
Convert BR tags to newlines.
<?php
/**
* Convert BR tags to newlines and carriage returns.
*
* @param string $string
* The string to convert
*
* @return string
* The converted string
@lonalore
lonalore / split_string_on_img.php
Created August 16, 2019 14:08
PHP split string on <img> tag
<?php
$string = 'Text <img src="hello.png" > hello <img src="bye.png" /> other text.';
$array = preg_split('/(<img[^>]+\>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
// array(5) {
// [0]=>
// string(5) "Text "
// [1]=>
@lonalore
lonalore / img_src_dom.php
Created August 16, 2019 13:55
PHP: get image src attribute (dom)
<?php
$doc = new DOMDocument();
$doc->loadHTML($html);
$xml = simplexml_import_dom($doc);
$images = $xml->xpath('//img');
foreach ($images as $img) {
@lonalore
lonalore / img_src_regexp.php
Created August 16, 2019 13:52
PHP: get image src attribute (regular expression)
<?php
$html = '<img border="0" src="/images/image1.jpg" alt="Image" width="100" height="100" />
<img border="0" src="/images/image2.jpg" alt="Image" width="100" height="100" />
<img border="0" src="/images/image3.jpg" alt="Image" width="100" height="100" />';
preg_match_all('@src="([^"]+)"@', $html, $match);
$src = array_pop($match);
<?php
/**
* Some distributions of Linux (most notably Debian) ship their PHP
* installations with garbage collection (gc) disabled. This depends on
* PHP's garbage collection for clearing sessions, ensure that garbage
* collection occurs by using the most common settings.
*/
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 100);
@lonalore
lonalore / TAJ_szam_validalas.php
Created June 15, 2018 12:53
TAJ szám validálás (Validation function for hungarian Social Security Number (TAJ))
<?php
/**
* Validation function for hungarian Social Security Number (TAJ).
*
* @param string $ssn
* The hungarian Social Security Number (TAJ).
*
* @return bool
* TRUE, if the number is valid. Otherwise FALSE.

1, Create /etc/ssh/login-notify.sh with the following contents:

#!/bin/sh

# Change these two lines:
sender="noreply@example.com"
recepient="notify-address@example.com"

if [ "$PAM_TYPE" != "close_session" ]; then
@lonalore
lonalore / DateRecurCalendar.php
Last active September 22, 2017 09:34
[CUSTOM_MODULE]/src/Plugin/views/style/DateRecurCalendar.php
<?php
namespace Drupal\[CUSTOM_MODULE]\Plugin\views\style;
use Drupal\[CUSTOM_MODULE]\Plugin\views\row\DateRecurCalendar as CalendarRowRecur;
use Drupal\calendar\CalendarDateInfo;
use Drupal\calendar\CalendarHelper;
use Drupal\calendar\CalendarStyleInfo;
use Drupal\views\Entity\View;
use Drupal\Core\Datetime\DateFormatter;
@lonalore
lonalore / DateRecurCalendar.php
Last active September 22, 2017 09:35
[CUSTOM_MODULE]/src/Plugin/views/row/DateRecurCalendar.php
<?php
namespace Drupal\[CUSTOM_MODULE]\Plugin\views\row;
use DateTimeZone;
use Drupal\calendar\CalendarEvent;
use Drupal\calendar\CalendarHelper;
use Drupal\calendar\CalendarViewsTrait;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityFieldManagerInterface;