Jul
PHP String Functions
Filed Under (PHP) by khawaib on 12-07-2010
To search a string in a text:
$string = "Ahmed";
$text = "My name is Ahmed.";
if(strstr($text, $string)) {
echo "found it.";
} else {
echo "not found.";
}
Jul
To search a string in a text:
$string = "Ahmed";
$text = "My name is Ahmed.";
if(strstr($text, $string)) {
echo "found it.";
} else {
echo "not found.";
}
Dec
Some servers have limit on memory for PHP which leads to errors when you start adding new extensions. Memory limit is set in PHP ini file.
PHP ini file can be located at different locations, easiest way to find the location is to run follwoing script on server.
<?php phpinfo(); ?>
Open the file through ftp or any other way easy for you.
Search for memory_limit word in file and increase its value, 64MB should be fine.
Restart Appache
You can also include following line before PHP closing tag in configuration file located in Joomla root directory to increase memory limit.
ini_set('memory_limit', '32M');
Dec
Recently one of my client asked me to add the functionality for users of their custom Content Management System(CMS) to give them more control over layouts etc. Using CSS in PHP makes it really easy to have styling flexibility.
Following steps are involved to write CSS via PHP.
Create CSS file as a PHP file
Set its content type using the following code
header("Content-type: text/css; charset: UTF-8");
Create PHP variables as you would normally in other PHP code. You can also bring variable data from database etc.
<?php $myVariable = '#000000'; $myLeftColHeight = '500px'; ?>
Outside your PHP tags you can write all of normal CSS code. Where ever you need to write the variable value just echo that variable not PHP way.
#myLeftCol {height:; color:;}
Final Code for stylesheet.php:
header("Content-type: text/css; charset: UTF-8");
$myVariable = '#000000';
$myLeftColHeight = '500px';
?>
#myLeftCol {height:; color:;}
Nov
Handy function to parse XML and other strings and get the inner string out.
Example 1:
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
$fullstring = "this is my [tag]dog[/tag]";
$parsed = get_string_between($fullstring, "[tag]", "[/tag]");
echo $parsed; // (result = dog)
Example 2:
function get_all_strings_between($string,$start,$end) {
//Returns an array of all values which are between two tags in a set of data
$strings = array();
$startPos = 0;
$i = 0;
//echo strlen($string)."\n";
while($startPos < strlen($string) && $matched = get_string_between(substr($string,$startPos),$start,$end)) {
if ($matched == null || $matched[1] == null || $matched[1] == '') break;
$startPos = $matched[0]+$startPos+1;
array_push($strings,$matched[1]);
$i++;
}
return $strings;
}
function get_string_between($string, $start, $end) {
//$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return null;
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return array($ini+$len,substr($string,$ini,$len));
}