What?
This is a quick note on how to reduce a whole bunch of CSS into a single line without unnecessary spaces and new lines.

Why?
What I have:

copyraw
#copyright a{
    margin: 10px 0 0 85px;
    box-shadow: 5px 5px 5px 0px rgba(51, 51, 51, 0.3);
}
  1.  #copyright a{ 
  2.      margin: 10px 0 0 85px
  3.      box-shadow: 5px 5px 5px 0px rgba(51, 51, 51, 0.3)
  4.  } 

What I want:

copyraw
#copyright a{margin:10px 0 0 85px;box-shadow:5px 5px 5px 0px rgba(51,51,51,0.3);}
  1.  #copyright a{margin:10px 0 0 85px;box-shadow:5px 5px 5px 0px rgba(51,51,51,0.3);} 


How?
So I'm doing this with a regular expression to get rid of newlines:

copyraw
$v_AppStyle = "
#copyright a{
    margin: 10px 0 0 85px;
    box-shadow: 5px 5px 5px 0px rgba(51, 51, 51, 0.3);
}";
$v_AppStyleFormatted = preg_replace('/\s+/', ' ', $v_AppStyle);
  1.  $v_AppStyle = " 
  2.  #copyright a{ 
  3.      margin: 10px 0 0 85px
  4.      box-shadow: 5px 5px 5px 0px rgba(51, 51, 51, 0.3)
  5.  }"
  6.  $v_AppStyleFormatted = preg_replace('/\s+/', ' ', $v_AppStyle)

and a few str_replace arrays:

copyraw
// exceptions
$a_ReplaceFrom1 = array("px ", "0 ", " a");
$a_ReplaceTo1 = array("px?", "0?", "?a");
$v_AppStyleFormatted = str_replace($a_ReplaceFrom1, $a_ReplaceTo1, $v_AppStyleFormatted);

// replace all spaces to empty and replace question marks back to spaces
$a_ReplaceFrom2 = array(" ", "?");
$a_ReplaceTo2 = array("", " ");
$v_AppStyleFormatted = str_replace($a_ReplaceFrom2, $a_ReplaceTo2, $v_AppStyleFormatted);
echo $v_AppStyleFormatted;
  1.  // exceptions 
  2.  $a_ReplaceFrom1 = array("px ", "0 ", " a")
  3.  $a_ReplaceTo1 = array("px?", "0?", "?a")
  4.  $v_AppStyleFormatted = str_replace($a_ReplaceFrom1, $a_ReplaceTo1, $v_AppStyleFormatted)
  5.   
  6.  // replace all spaces to empty and replace question marks back to spaces 
  7.  $a_ReplaceFrom2 = array(" ", "?")
  8.  $a_ReplaceTo2 = array("", " ")
  9.  $v_AppStyleFormatted = str_replace($a_ReplaceFrom2, $a_ReplaceTo2, $v_AppStyleFormatted)
  10.  echo $v_AppStyleFormatted
Category: Personal Home Page :: Article: 697

What?
Just a quick note to refine a function that will take a date in the past and return the number of years, months, weeks, days, hours, minutes and seconds.

Why?
Here are some examples of what we want to achieve:
copyraw
1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds  // full string
1 year 3 weeks 5 hours 7 seconds                            // where shown values are not zero
  1.  1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds  // full string 
  2.  1 year 3 weeks 5 hours 7 seconds                            // where shown values are not zero 
Actually we just want the first two words of those strings:
copyraw
3 days ago          // where event is 3 days ago or just over but less than 4 days ago
1 week ago          // where event is just over 1 week ago but less than 2 weeks ago
  1.  3 days ago          // where event is 3 days ago or just over but less than 4 days ago 
  2.  1 week ago          // where event is just over 1 week ago but less than 2 weeks ago 

How?
Category: Personal Home Page :: Article: 679

What?
So this is an article for me on how to copy Joomla's and Wordpress' feature where a Title/Name value is converted to a search-engine friendly URL alias and is unique in the database. Ok that's a long sentence; let me try this:
  1. When I save a record in JoomlaCMS or WordpressCMS
  2. It creates a unique name to use in URLs
  3. I want that

Why?
These are used as inputs to server-side scripts for the sake of search-engine friendliness. There are no silver bullets here but I want to block any character that isn't a letter, a number or an underscore.

How?
Suppose the following exists as a MySQL database table called my_table_name:

What we have:
copyraw
/----------|-------------------|----------------------\
| id       | name              | url_alias            |
|----------|-------------------|----------------------|
| 1        | My *First* Test   | my_first_test        |
|          |                   |                      |
|          |                   |                      |
\----------|-------------------|----------------------/
  1.  /----------|-------------------|----------------------\ 
  2.  | id       | name              | url_alias            | 
  3.  |----------|-------------------|----------------------| 
  4.  | 1        | My *First* Test   | my_first_test        | 
  5.  |          |                   |                      | 
  6.  |          |                   |                      | 
  7.  \----------|-------------------|----------------------/ 
What we want:
copyraw
/----------|-------------------|----------------------\
| id       | name              | url_alias            |
|----------|-------------------|----------------------|
| 1        | My *First* Test   | my_first_test        |
| 2        | My *First* Test   | my_first_test_1      |
| 3        | My _-_First Test  | my_first_test_2      |
\----------|-------------------|----------------------/
  1.  /----------|-------------------|----------------------\ 
  2.  | id       | name              | url_alias            | 
  3.  |----------|-------------------|----------------------| 
  4.  | 1        | My *First* Test   | my_first_test        | 
  5.  | 2        | My *First* Test   | my_first_test_1      | 
  6.  | 3        | My _-_First Test  | my_first_test_2      | 
  7.  \----------|-------------------|----------------------/ 

Category: Personal Home Page :: Article: 677

What?
A quick article to stop me running into this issue again. This article serves to address the issue of importing characters from an XML in a different language character set and trying to load it in PHP with the function simplexml_load_string(). The error I get is something similar to:

PHP Warning:
simplexml_load_string(): Entity: line #: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xA0 0x3C 0x2F 0x73 in /home/public_html/my_folder/my_xml_processing_script.php on line 160


Why?
I'm downloading an XML feed to our servers, and then loading the downloaded file into memory with simplexml_load_string(). I get the above error when it is attempting to load an XML feed which is mostly in Spanish and breaks at the following XML node:

copyraw
<baños>2</baños>

-> yields issue: PHP Warning:  simplexml_load_string():     <baños>2</baños> in /home/public_html/my_folder/my_xml_processing_script.php on line 160

should read

<baños>2</baños>
  1.  <baños>2</baños> 
  2.   
  3.  -> yields issue: PHP Warning:  simplexml_load_string():     <baños>2</baños> in /home/public_html/my_folder/my_xml_processing_script.php on line 160 
  4.   
  5.  should read 
  6.   
  7.  <baños>2</baños> 


How?

Category: Personal Home Page :: Article: 642

Previously titled
Fix PHP cURL: parser error: Document labelled UTF-16 but has UTF-8 content

What?
This is an article with notes for me on how to convert some received XML encoded in UTF-16 to some JSON in UTF-8. If it were entirely in UTF-8, I would simply load the received XML with SimpleXML and use the built-in PHP JSON_encode function. I ran into the following errors:
Warning: SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]: Entity: line 1: parser error : Document labelled UTF-16 but has UTF-8 content in /public_html/.../.../my_script.php on line ###

Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: Entity: line 1: parser error : Document labelled UTF-16 but has UTF-8 content in /public_html/.../.../my_script.php on line ###
Why?
So I've googled, binged and yahoo'd for this and although there are some solutions that deal with loading UTF16 content into SimpleXMLElement or simplexml_load_string, it doesn't solve my problem. I'm receiving XML data within a cURL result but I get the above error with using either "SimpleXMLElement" or "simplexml_load_string". Returning the XML with cURL isn't a problem, but I want to convert it to JSON and I usually use a PHP function to load the data into an XML array and use the built-in PHP function: "json_encode".

How?

What?
A note for myself on some code to convert a string of two names into a string made up of the first name and then using the initial of the second name.
copyraw
-- What I have
John Smith
Fred.Bloggs

-- What I want
John S.
Fred B.
  1.  -- What I have 
  2.  John Smith 
  3.  Fred.Bloggs 
  4.   
  5.  -- What I want 
  6.  John S. 
  7.  Fred B. 

How?
Category: Personal Home Page :: Article: 556

What?
A quick note on a htaccess rewrite rule I'm liking.

What does it do?
What I type:
copyraw
http://www.mywebsite.com/blog/videos.html
  1.  http://www.mywebsite.com/blog/videos.html 
Sends this to server:
copyraw
http://www.mywebsite.com/index.php?myFolder=blog&myFiles=videos
  1.  http://www.mywebsite.com/index.php?myFolder=blog&myFiles=videos 
How?
Category: Personal Home Page :: Article: 520

What?
For those of you who use Preg_Replace. Preg_replace is a function that uses regular expressions to search and replace a string.

Why?
Because my understanding with regular expressions is shady and varies from language to language, I've written this article as a quick reference point.

How?

What?
Just a quick note on how to format a given filesize and to reduce the display output to a small string, eg:
copyraw
196 bytes          : displays as => "196 bytes"
   12945 bytes        : displays as => "12 Kb"
   1478515 bytes      : displays as => "1 Mb"
   8798745455 bytes   : displays as => "8 Gb"
  1.  196 bytes          : displays as => "196 bytes" 
  2.     12945 bytes        : displays as => "12 Kb" 
  3.     1478515 bytes      : displays as => "1 Mb" 
  4.     8798745455 bytes   : displays as => "8 Gb" 

Category: Personal Home Page :: Article: 468

What?
This is intended for activity/session durations and although I usually get MySQL to do the date/time calculations, there are times when we have to do with PHP. This is the shortest way I know to properly convert seconds into total hours, minutes and seconds (taking into account regional settings and without using a date function).

How?
copyraw
$total_time =intval(intval($total_seconds)/ 3600).":";
$total_time.=str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":";
$total_time.=str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT);

// yields
// 82800  = 23:00:00
// 108000 = 30:00:00
  1.  $total_time =intval(intval($total_seconds)3600).":"
  2.  $total_time.=str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":"
  3.  $total_time.=str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT)
  4.   
  5.  // yields 
  6.  // 82800  = 23:00:00 
  7.  // 108000 = 30:00:00 
On one line:
copyraw
$total_time=intval(intval($total_seconds)/ 3600).":".str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":".str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT);
  1.  $total_time=intval(intval($total_seconds)3600).":".str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":".str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT)
Category: Personal Home Page :: Article: 413

So I know it's quite a long title but I'm not sure what I'll be searching next time. It has taken me a lot longer than I thought it would mostly out of frustration and the inability to know exactly what I'm looking for... always difficult. After a cup of tea the solution was glaringly obvious, just do a primary school mathematics table and it all makes sense (see my 10 mod table below).

This article is a quick note (so I never spend as long again) in PHP on how to determine when looping through a loop, which entry was first and which was last. This is incredibly useful for pagination.


Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

RSS Feed

Related Articles

Joes Revolver Map

Joes Word Cloud

creator   display   windows   create   uploaded   system   list   where   function   google   deluge   need   first   field   zoho   server   error   mysql   name   parameter   would   user   note   added   page   script   form   order   using   client   find   work   database   files   license   data   report   file   value   used   case   time   version   table   following   joomla   website   source   code   date   JoelLipman.Com

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.