From The Blog

Drupal: Form Validation Messages Order

27 July 2017, in

Hello all Drupalists,

Have you ever had a problem with form validation messages order, ... I think we all had this issue.

For some reason validation messages are not ordered the right way (with the same order as the form fields).

For this reason, I wrote the below code to be used to reorder form validation messages.

Note that the below code also has a function to be used if you are using conditional fields module to manage field dependencies.

 



Post a Comment

Javascript: have variable variables

26 November 2013, in

In PHP to have "Variable" variables.

For example

$x = "variable";

$$x = "hello, world!";

echo $variable;" //"hello, world!"

So as a workaround you can do as the following:

var obj = {}
obj.foo = 42;

var bar = 'foo';
console.log(obj[bar]);


Post a Comment

PHP - Ubuntu: supported locales

26 November 2013, in

1. First of all list supported locales:

cat /usr/share/i18n/SUPPORTED

2. Copy the locales for your region. I will copy the following lines:

sv_SE.UTF-8 UTF-8

sv_SE ISO-8859-1

3. Edit the list of 'supported locales' and paste your locales that you've copied. (See step above)

gedit /var/lib/locales/supported.d/local

 



Post a Comment

PHP: Problem getting date in different locale

26 November 2013, in

Have you ever faced a problem trying to get date in another locale ?

The way you can do it right is as follows:

You will use set setlocale(), strftime()

suppose you want to get the date in Italian

setlocale(LC_ALL, 'it_IT'); // Set locale to Italian

echo strftime("%e %B %Y"); //today's date

echo strftime("%e %B %Y",strtotime($date)); //another date



Post a Comment

PHP: Regex match anchor text and url in a string

12 November 2013, in

To match anchor text and url in a string:

$sourcestring = 'this is a good test <strong><a href="http://wordpress.org">file (.docx)</a></strong> 
and this is the new <a href="wp-content/uploads/amir.jpg">site image</a> ';

preg_match_all('/<a[^>]+href=[\'|"]([^>]+)[^>]+>([^<]+)<\/a>/i',$sourcestring,$matches);

var_dump($matches);



Post a Comment

Get the sources of img tags from html

21 October 2013, in

/**
* get the sources of img tags from html input parameter
*/
function get_html_img_sources($html){
//$html = types_render_field( "gallery_image", array( "size" => "medium", "proportional" => "true",'output'=>'raw') );
if(empty($html))
return array();
$doc = new DOMDocument();
$doc->loadHTML($html);
$imageTags = $doc->getElementsByTagName('img');



Post a Comment

PHP: check whether an AJAX request

5 June 2013, in

if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) {

 

//if ajax request your code runs here

}

else{

//if not an ajax request your code runs here

}



Post a Comment

PHP: make the cookies work globally on all subdomains

2 January 2013, in

To make cookies work globally on subdomains of a domain:

setcookie('CookieName', 'Value', time() + 3600, '/', '.mydomain.com');

Note: 
remember to use the dot (.) in '.mydomain.com' not 'mydomain.com'


Post a Comment

CakePHP: change default model for a controller

30 November 2012, in

By default a CakePHP controller uses a model with singularized name, for example a controller with name Students will use by default a model named Student,

But if you want or have to change the default model for a controller you can set it using

$modelClass member attribute

for example for a controller named ClassesController and you can't name the model class with name "Class" because it is a keyword in PHP, and you decided to change the model name to Classroom

Class ClassesController extends AppController {

$modelClass = 'Classroom';

....



Post a Comment

PHP: check string has HTML tags

25 November 2012, in

you may think as the best way to check a string has HTML tags is to check the string after stripping tags:

if( strlen($string) != strlen(strip_tags($string)) ){

echo 'Contains HTML tags';

}

But, the best way is to use regular expressions:

if (preg_match("/([\<])([^\>]{1,})*([\>])/i", $string )) {
echo 'Contains HTML tags';
}



Post a Comment

Pages