Programming tips and tricks » PHP http://blog.programovani.net Useful programming tips and tricks Sat, 28 Jul 2012 18:04:38 +0000 en hourly 1 http://wordpress.org/?v=3.2.1 Special characters in PHP exported CSV http://blog.programovani.net/en/php/special-characters-export-to-csv/?utm_source=rss&utm_medium=rss&utm_campaign=export-do-csv-a-diakritka-php-a-utf-8 http://blog.programovani.net/en/php/special-characters-export-to-csv/#comments Sat, 28 Jul 2012 17:56:39 +0000 admin http://blog.programovani.net/?p=97 Continue reading ]]> When creating a CSV file with the special characters, be sure to encounter the problem of encoding characters. Although the output is in UTF-8 OK, Microsoft Excel displays the wrong characters. The solution is simple and is called BOM. At the beginning of the file just add the BOM characters, indicating that the file is in the character set UTF-8.

Solution:

$ document = chr (0xEF). chr (0xBB). chr (0xBF). $ csvContent;

Variable csvContent contains the contents of the CSV document. The variable document is a content of CSV dates, you just need to write to a file.

]]>
http://blog.programovani.net/en/php/special-characters-export-to-csv/feed/ 0
String contains another string http://blog.programovani.net/en/php/string-contains-another-string/?utm_source=rss&utm_medium=rss&utm_campaign=podminka-zda-retezec-obsahuje-druhy-retezec http://blog.programovani.net/en/php/string-contains-another-string/#comments Sun, 25 Sep 2011 16:32:12 +0000 admin http://blog.programovani.net/?p=89 Continue reading ]]> In this paper, I will bring a simple command (condition) in PHP to check whether a string contains another string. Unfortunately, PHP don’t have function contains, and we must create it yourself.

1st Testing whether a string contains a substring.

if (strpos($retezec,$hledany_vyraz) !== false) {
    echo 'String found';
}

2nd The condition whether a string contains a given substring.

if (strpos($retezec,$hledany_vyraz) === false) {
    echo 'String not found';
}

3rd A comprehensive test on the contents of the substring in string

if (strpos($retezec,$hledany_vyraz) === false) {
    echo 'string not found';
}  else {
    echo 'string found';
}

How does it works?
strpos returns the position of substring in the text. If the text does not contains substring, it returns false. As can be just the substring in the first place, you must use == instead of === operator (or !==).

Important Notice! Do not use for test commands strstr or ereg! Both commands are too computationally and memory intensive.

]]>
http://blog.programovani.net/en/php/string-contains-another-string/feed/ 0
Current page URL http://blog.programovani.net/en/php/current-page-url/?utm_source=rss&utm_medium=rss&utm_campaign=url-aktualni-stranky http://blog.programovani.net/en/php/current-page-url/#comments Sun, 25 Sep 2011 09:32:13 +0000 admin http://blog.programovani.net/?p=81 Continue reading ]]> Often it is necessary for PHP to find out what is the URL of the currently displayed page. If you are using only HTTP and not using a different port number than 80, you can use the following code:

$pageURL = "http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

If you are using HTTPS or port numbers, you can use the following more complex script:

$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {
     $pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
     $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
     $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
]]>
http://blog.programovani.net/en/php/current-page-url/feed/ 0