Showing posts with label PHP EXPLODE. Show all posts
Showing posts with label PHP EXPLODE. Show all posts

Thursday, 30 August 2018

Returns the part of a string before the first occurrence of a character in php

In PHP, what is the simplest way to return the portion of a string before the first occurrence of a specific character?

For example, if I have a string...
"The quick brown foxed jumped over the etc etc."
...and I am filtering for a space character (" "), the function would return "The"
Thanks!

You could do this:
$string = 'The quick brown fox jumped over the lazy dog';
$substring = substr($string, 0, strpos($string, ' '));

But I like this better:
list($firstWord) = explode(' ', $string);

Thursday, 4 June 2015

PHP: Functions used to define a schedule of holidays. Can define non-fixed holidays

(eg. 3rd sunday of June).

<?php
function GetTimeStamp($MySqlDate)
  {

    
  $date_array = explode("-",$MySqlDate); // split the array
    
  $var_year = $date_array[0];
  $var_month = $date_array[1];
  $var_day = $date_array[2];

  $var_timestamp = mktime(0,0,0,$var_month,$var_day,$var_year);
  return($var_timestamp); // return it to the user
  }  // End function GetTimeStamp()

function ordinalDay($ord, $day, $month, $year)
  // ordinalDay returns date of the $ord $day of $month.
  // For example ordinalDay(3, 'Sun', 5, 2001) returns the
  // date of the 3rd Sunday of May (ie. Mother's Day).
  //
  // Note: $day must be the 3 char abbr. for the day, as
  //       given by date("D");
  //

  {
  $firstOfMonth = GetTimeStamp("$year-$month-01");
  $lastOfMonth  = $firstOfMonth + date("t", $firstOfMonth) * 86400;
  $dayOccurs = 0;
    
  for ($i = $firstOfMonth; $i < $lastOfMonth ; $i += 86400)
     {
     if (date("D", $i) == $day)
       {
       $dayOccurs++;
       if ($dayOccurs == $ord)
         { $ordDay = $i; }
       }
     }
  return $ordDay;
  }  // End function ordinalDay()

function getNextHoliday()
   // Looks through a lists of defined holidays and tells you which
   // one is coming up next.
   //
   {
   $year = date("Y");

   class holiday
     {
     var $name;
     var $date;
     var $catNum;
            
     function holiday($name, $date, $catNum)
        // Contructor to define the details of each holiday as it is created.
        {
        $this->name   = $name;   // Official name of holiday
        $this->date   = $date;   // UNIX timestamp of date
        $this->catNum = $catNum; // category, we used for databases access
        }
     } // end class holiday
            
   $holidays[] = new holiday("Groundhog Day", GetTimeStamp("$year-2-2"), "20");
   $holidays[] = new holiday("Valentine's Day", GetTimeStamp("$year-2-14"), "14");
   $holidays[] = new holiday("St. Patrick's Day", GetTimeStamp("$year-3-17"), "15");
   $holidays[] = new holiday("Easter", easter_date($year), "16");
   $holidays[] = new holiday("Mother's Day", ordinalDay(2, 'Sun', 5, $year), "3");
   $holidays[] = new holiday("Father's Day", ordinalDay(3, 'Sun', 6, $year), "4");
   $holidays[] = new holiday("Independence Day", GetTimeStamp("$year-7-4"), "17");
   $holidays[] = new holiday("Christmas", GetTimeStamp("$year-12-25"), "13");

   $numHolidays = count($holidays);
   for ($i = 0; $i < $numHolidays; $i++)
     {
     if ( date("z") > date("z", $holidays[$i]->date) && date("z") <= date("z",
          $holidays[$i+1]->date) )
        {
        $nextHoliday["name"]      = $holidays[$i+1]->name;
        $nextHoliday["dateStamp"] = $holidays[$i+1]->date;
        $nextHoliday["dateText"]  = date("F j, Y", $nextHoliday["dateStamp"]);
        $nextHoliday["num"]       = $holidays[$i+1]->catNum;        
        }
     }
   return $nextHoliday;
   } // end function GetNextHoliday


$nextHoliday = getNextHoliday();
echo $nextHoliday["name"]." (".$nextHoliday["dateText"].")";

?>

Tuesday, 2 June 2015

PHP: Is IP Is it a valid IP?

<?php
function is_ip($text) {
 foreach (explode(".", $text) as $num) { if ($num > 255) return false; } return true;
}
?>

Usage
Although anything above 224.* and the private and loopback addresses are probably not useful, they're still IP'.

Monday, 23 February 2015

PHP: Detect browser language

If your website is multilingual, it can be useful to detect the browser language to use this language as the default. The code below will return the language used by the client’s browser.

function get_client_language($availableLanguages, $default='en'){
 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  $langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);

  foreach ($langs as $value){
   $choice=substr($value,0,2);
   if(in_array($choice, $availableLanguages)){
    return $choice;
   }
  }
 } 
 return $default;

PHP: Extract keywords from a webpage

The title said it all: A great code snippet to easily extract meta keywords from any webpage.
<?php
$meta = get_meta_tags('https://thiscode4u.blogspot.com/');
$keywords = $meta['keywords'];
// Split keywords
$keywords = explode(',', $keywords );
// Trim them
$keywords = array_map( 'trim', $keywords );
// Remove empty values
$keywords = array_filter( $keywords );

print_r( $keywords );
?>

Thursday, 25 September 2014

explode in PHP

The PHP explode() function is utilized to returns an array formed from a specified string.
The PHP explode() function is easily remembered as "string to array", which simply means that it takes an string and returns an array.

Syntax:

explode (separator,string,limit)
separator : Required. Points out where to break the string.
string : Required. The input string.
limit : Optional. Maximum number of array elements to return.
Note : Separator can not be an empty string and It is binary safe.

Example:

<?php 
$input_str = "Good Morning. It's a Friday today.";
print_r(explode(" ",$input_str));
?>

O/P:

Array (
          [0] => Good
          [1] => Morning.
          [2] => It's 
          [3] => a 
          [4] => Friday 
          [5] => today. 
        )