Displaying 11-20 of 37 results.
Resolved: PHP get the first word of a sentence string
posted by admin on May 22, 2016
I want to extract the first word of a variable from a string. For example, take this input:

<?php $myvalue = 'Test me more'; ?>

The resultant output should be Test, which is the first word of the input. How can I do this?Read more
Wiki: PHP strtotime() Functionwiki
posted by admin on May 22, 2016
Example
Parse English textual datetimes into Unix timestamps:

<?php
echo(strtotime("now") . "
");
echo(strtotime("3 October 2005") . "
");
echo(strtotime("+5 hours") . "
");
echo(strtotime("+1 week") . "
");
echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "
");
echo(strtotime("next Monday") . "
");
echo(strtotime("last Sunday"));
?>

Definition and Usage
The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

Note: Be aware of dates in the m/d/y or d-m-y formats; if the separator is a slash (/), then the American m/d/y is assumed. If the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential errors, you should YYYY-MM-DD dates or date_create_from_format() when possible.

Syntax

strtotime(time,now);


Parameter Description
time Required. Specifies a date/time string
now Optional. Specifies the timestamp used as a base for the calculation of relative dates

Technical Details
Return Value: Returns a timestamp on success. FALSE on failure
PHP Version: 4+
Changelog: PHP 5.3.0: Now relative time formats such as this week, previous week, last week, and next week interprets a week period of Monday through Sunday, rather then a 7-day period relative to the current date/time
PHP 5.3.0: Now 24:00 is a valid format
PHP 5.2.7: In earlier versions, if requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month it would incorrectly add one week to the returned timestamp. This has been corrected now
PHP 5.1.0: Returns FALSE on failure (earlier versions returns -1), and issues E_STRICT and E_NOTICE time zone errors
PHP 5.0.2: Now correctly computes "now" and other relative times from current time, not from today's midnight
PHP 5.0.0: Allows microseconds (but they are ignored)Read more
Resolved: PHP compose class property by string and value
posted by admin on May 22, 2016
I have object or class.

class Class1{
public $language = "English";
public $languageru = "Russian";

public function composeValue(){
//return my value, or empty or "ru"
}
}

$class = new Class1();
$value = $class->composeValue();
// I want something like this
echo $class->language.$value
//But it is not working
Read more
Resolved: Remove the last character(comma) from string
posted by admin on May 6, 2016
I have a string like

a,b,c,d,e,


I would like to remove last ',' and get the remaining string back

OUTPUT: a,b,c,d,e
Read more
Resolved: PHP display an Array under alphabetical letters without key sensitive
posted by admin on April 27, 2016
I have an associative array of tags like

array(11) {
[0]=>
array(4) {
["tag_id"]=>
string(2) "81"
["slug"]=>
string(4) "mag1"
["name"]=>
string(4) "mag1"
["TagsCount"]=>
string(1) "2"
}
[1]=>
array(4) {
["tag_id"]=>
string(2) "85"
["slug"]=>
string(4) "mag2"
["name"]=>
string(4) "mag2"
["TagsCount"]=>
string(1) "1"
}
[2]=>
array(4) {
["tag_id"]=>
string(2) "83"
["slug"]=>
string(4) "mag3"
["name"]=>
string(4) "mag3"
["TagsCount"]=>
string(1) "2"
}
[3]=>
array(4) {
["tag_id"]=>
string(2) "84"
["slug"]=>
string(4) "mag4"
["name"]=>
string(4) "mag4"
["TagsCount"]=>
string(1) "2"
}
. . .
[9]=>
array(4) {
["tag_id"]=>
string(2) "79"
["slug"]=>
string(4) "tag5"
["name"]=>
string(4) "tag5"
["TagsCount"]=>
string(1) "1"
}
[10]=>
array(4) {
["tag_id"]=>
string(2) "80"
["slug"]=>
string(4) "tag6"
["name"]=>
string(4) "tag6"
["TagsCount"]=>
string(1) "1"
}
}

In output I want to see result like

<h3>A</h3>
<ol>
<li>
<em>tag count</em>
<a href="link to tag">Animal</a>
</li>
<li>
<em>tag count</em>
<a href="link to tag">Aqua</a>
</li>
<li>
<em>tag count</em>
<a href="link to tag">Arthur</a>
</li>
</ol>
<!-- if B not EXIST not show B -->
<h3>C</h3>
<ol>
<li>
<em>tag count</em>
<a href="link to tag">Camel</a>
</li>
<li>
<em>tag count</em>
<a href="link to tag">Crazy</a>
</li>
</ol>
<!-- etc -->
Read more
Wiki: PHP 5 Sorting Arrayswiki
posted by admin on April 16, 2016
The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.

PHP - Sort Functions For Arrays

In this chapter, we will go through the following PHP array sort functions:
  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order, according to the value
  • ksort() - sort associative arrays in ascending order, according to the key
  • arsort() - sort associative arrays in descending order, according to the value
  • krsort() - sort associative arrays in descending order, according to the key


Sort Array in Ascending Order - sort()
The following example sorts the elements of the $cars array in ascending alphabetical order:

<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>


The following example sorts the elements of the $numbers array in ascending numerical order:

<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>


Sort Array in Descending Order - rsort()
The following example sorts the elements of the $cars array in descending alphabetical order:

<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>


The following example sorts the elements of the $numbers array in descending numerical order:

<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>


Sort Array (Ascending Order), According to Value - asort()
The following example sorts an associative array in ascending order, according to the value:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>


Sort Array (Ascending Order), According to Key - ksort()
The following example sorts an associative array in ascending order, according to the key:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>


Sort Array (Descending Order), According to Value - arsort()
The following example sorts an associative array in descending order, according to
arsort($age);
?>
[/php]

Sort Array (Descending Order), According to Key - krsort()
The following example sorts an associative array in descending order, according to the key:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>
Read more
Wiki: PHP Dominant Colour Function with GD Librarywiki
posted by admin on April 13, 2016
Someone asked if it was possible to find the dominant colour (the more frequent colour, in %) of an image.

So I read about the GD PHP Library, which enables image support. I found the interesting function imageColorAt() which the colour of a specific pixel. Hence, my function consists of resizing to a known size, 100 x 100, then looping into each pixel, storing it’s colour into an array, then sorting the array to find the more frequent key.

Here’s the result :

function getDominantColour($image, $type='jpeg') // $type can be gif, jpeg or png
{
// This function needs GD Library enabled

eval('$source = imagecreatefrom' . $type . '("' . $image . '");'); // we load the source image
$source_x = imagesx($source); // source's X
$source_y = imagesy($source); // source's Y
$resized_x = 100; // resized image's X
$resized_y = 100; // resized image's Y
$resized= imagecreatetruecolor($resized_x, $resized_y); // we create a blank 100 x 100 image
imagecopyresampled($resized, $source, 0, 0, 0, 0, $resized_x, $resized_y, $source_x, $source_y); // We then copy and resized the source image into the blank image

// Required vars declaration
$colours = array();
$rgb = '';
$index = array();
for ($x=0; $x<100; $x++) // We loop into the pixels X
{
for ($y=0; $y<100; $y++) // We loop into the pixels Y
{
$rgb = imagecolorat($resized, $x, $y); // get the RGB of the pixel
$index = imagecolorsforindex($resized, $rgb); // Get the RGB Index
$key = 'R' . $index['red'] . 'G' . $index['green'] . 'B' . $index['blue']; // Put it into a string
if (empty($colours[$key])) // If it's the first time this colour is encountered
{
$colours[$key] = 1; // We give it a value of 1
} else { // If it's not the first time it is encountered
$colours[$key]++; // We increment its value
}
}
}
arsort($colours, SORT_NUMERIC); // We then sort the colours array so that the first one is the most frequent one
return key($colours); // we return the most frequent value
}


Working with images sure is fun. Looking forward to have more functions with the GD libraryRead more
Wiki: PHP Convert Hex Color to RGB and RGB to Hexwiki
posted by admin on April 13, 2016
Converting certain values back and forth is often necessary when developing functions to help your website work the way you want it to. I needed a function that would convert a hex color to rgb and for some reason it took me a while to figure it out.

function hex2rgb($hex) {
$hex = str_replace("#", "", $hex);

if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
$rgb = array($r, $g, $b);
//return implode(",", $rgb); // returns the rgb values separated by commas
return $rgb; // returns an array with the rgb values
}


This function works with both shorthand hex codes such as #f00 and longhand hex codes such as #ff0000. It also accepts the number sign (#) just in case. You can see there are two return lines at the end of the function. The first, which is commented out, will return the rgb values separated by a comma. The second, which is the default, will return an array with the rgb values.

So now with this function in place we can use it like so:

$rgb = hex2rgb("#cc0");
print_r($rgb);


The above function would output:

Array ( [0] => 204 [1] => 204 [2] => 0 )


Since we are going one way with this, might as well go the other. Here is a function to convert rgb to a hex color:

function rgb2hex($rgb) {
$hex = "#";
$hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);

return $hex; // returns the hex value including the number sign (#)
}


This function works like so:

$rgb = array( 255, 255, 255 );
$hex = rgb2hex($rgb);
echo $hex;


The above function would output:

#ffffff
Read more
Resolved: Convert an associative array to a simple array of its values in php
posted by admin on April 7, 2016
I would like to convert the array:

Array (
[category] => category
[post_tag] => post_tag
[nav_menu] => nav_menu
[link_category] => link_category
[post_format] => post_format
)

to

array(category, post_tag, nav_menu, link_category, post_format)
Read more
Resolved: PHP remove same values from two arrays
posted by admin on April 5, 2016
I have two arrays
1.

$array1 = array("a" => "green", "red", "blue", "red");


2.

$array2 = array("b" => "green", "yellow", "red");


I want to compare values, and get new array with unique elements.
Result

Array
(
[1] => blue
)
Read more