How to parse a string between two strings

Filed Under (PHP) by khawaib on 28-11-2009

Tagged Under : , , , , , , , , ,

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));
}

Joomla usefull variables

Filed Under (Joomla! 1.5) by khawaib on 27-11-2009

Tagged Under : , , , ,

A list of useful Joomla variables I often need.

$mainframe->getCfg('offset') - returns the time offset configured in Joomla Global settings.
$mainframe->getCfg('live_site') - returns live site configured in Joomla Global settings.
JURI::base()

How to reset Joomla Super Admin password

Filed Under (Joomla! 1.5) by khawaib on 24-11-2009

Tagged Under : , , , , , , , , , , , , , , , , ,

To reset password you must have access to Joomla database. Most of the hosting providers come with some sort of control panel or PhpMyAdmin. Also make a backup of your database in case you have to revert back to original database.

Find users table, e.g. jos_users. Note you table names may vary depending on the prefix used with table names (e.g. prefix_users).

Find the Super Administrator account. It will have usetype = “Super Administrator”

Using mySQL’s builtin MD5 function we can set the new password using following SQL statement.

UPDATE `jos_users` SET `password` = MD5( 'newPassword' ) WHERE `jos_users`.`username` = "admin";

Now goto www.yourwebsite.com/administrator and login to backend using your Super Admin username.

How to find a file in Eclipse using file name

Filed Under (Eclipse) by khawaib on 20-11-2009

Tagged Under : , , , , , , ,

Eclipse comes with advanced search capabilities. User can drill down in file explorer view but using search can make it really easy.

Use Ctrl+Shift+R to open Open Resource dialog box which will let you type in any file name you are looking for. Results will starts appearing as you start to type file name. You can click to open any file you want from the results. This is the fasted way to find any resource (files).

To search any text in any file on project hit Ctrl+H and it will open a search dialog box where you have many different search options to choose from.

Joomla Constants

Filed Under (Joomla! 1.5) by khawaib on 19-11-2009

Tagged Under : , , , , ,

Following is list of constants that can be used in Joomla:

JPATH_ADMINISTRATOR 	The path to the administrator folder.
JPATH_BASE 	The path to the installed Joomla! site.
JPATH_CACHE 	The path to the cache folder.
JPATH_COMPONENT 	The path to the current component being executed.
JPATH_CONFIGURATION 	The path to folder containing the configuration.php file.
JPATH_INSTALLATION 	The path to the installation folder.
JPATH_LIBRARIES 	The path to the libraries folder.
JPATH_PLUGINS 	The path to the plugins folder.
JPATH_ROOT 	The path to the installed Joomla! site.
JPATH_SITE 	The path to the installed Joomla! site.
JPATH_THEMES 	The path to the templates folder.
JPATH_XMLRPC 	The path to the XML-RPC Web service folder.

These constants are defined in _path_/includes/defines.php except JPATH_BASE which is defined in _path_/index.php.

Note: These paths are the absolute paths of these locations within the file system, NOT the path you'd use in a URL.

Also <a href="http://docs.joomla.org/JURI/base">JURI/base</a>.<ins datetime="2009-11-19T00:58:51+00:00"></ins>

/* this gives you the root of your url, something like http://www.mydomain.com/whereverjoomlais/       */
JURI::root();

/* this one gives you the url to your index something like http://www.mydomain.com/whereverjoomlais/ if you are in front-end, and http://www.mydomain.com/whereverjoomlais/administrator/ if in back-end */
JURI::base();

/* Same but without the domain (like this: /wherejoomlais/administrator ) NOTE: this one is without the trailing "/"  */
JURI::base( true );

/* Path to your root directory, "/opt/xampp/..." or similar in Linux, "C:\wampp\..." on Windows */
var_dump( JPATH_ROOT );

/* Path to your admin directory, "/opt/xampp/joomla155/administrator" or similar in Linux, "C:\wampp\joomla155\administrator" on Windows. */
var_dump( JPATH_ADMINISTRATOR );

/* Path to your current component directory, "/opt/xampp/joomla155/administrator/components/com_free" or similar in Linux, "C:\wampp\joomla155\administrator\components\com_free" on Windows. */
var_dump( JPATH_COMPONENT );

/* Your directory separator: "/" in Linux, "\" on windows; use this to create a path to a directory so you don't have to worry about the server OS you're using */
var_dump( DS );