• t
  • g
  • B
  • Z
  • @
  • e

portfolio & blog of senior web developer, Fahd Murtaza



  • About
  • Portfolio
  • WordPress
  • CV
  • Blog
  • Contact

  • About
  • Portfolio
  • WordPress
  • CV
  • Blog
  • Contact

PHP: Removing last instance of a character within text using Regex

August 28th, 2011 by Fahd

I recently encountered a situation where I needed to remove the last instance of the character ‘|’ in the menu that was being dynamically generated. So I came up with the following code

<?
$menu = 'I AM |NOT| A BAD |BOY|';
$menu = preg_replace('/[|]([^|]*$)/', '$2', $menu);
echo $menu."\n";
?>

The output would be

I AM |NOT| A BAD |BOY

And that removes the last instance of a character (in this case a pipe sign, i.e ‘|’ ). Hope that helps others.

Posted in PHP

3 Comments

Best way to get file extension in PHP

July 25th, 2010 by Fahd

I tried a 100 different ways ( not literally) to get the file extension from a complete file path including directories and file name with extension. This one works perfectly. This uses php function pathinfo to get the directory name, filename and its extension for you which is very handy.

<?php
$fileinfo = pathinfo(“/path/to/your/file.php”);
echo $fileinfo['dirname']; // Get directory
echo $fileinfo['basename']; // Get file basename
echo $fileinfo['extension']; // Get file extension
echo $fileinfo['filename']; // Get file name
?>

Posted in PHP

3 Comments

Testing zend optimizer encoded PHP applications

July 4th, 2010 by Fahd

I am working on an expressionengine application these days. As I am using the beta version of expression engine until they release the final version, so the source files are encoded with the zend optimizer. Zend optimizer has a limitation that it can’t work with version 5.3 of PHP so far.

I had the latest XAMPP installed on my Windows VISTA machine for a year now but I found out that Zend optimiser isn’t compatible with version of PHP it provided i.e PHP 5.3, so I downloaded the version 1.6.8 of XAMPP from http://sourceforge.net/projects/xampp/files/ which has the older version of PHP i.e 5.2 which is compatible with Zend Optimizer. (more…)

Posted in PHP

Leave a comment

PHP: Text cropping with defining the number of characters, words or sentences

March 18th, 2009 by Fahd

I have recently been working on PHP RSS reader script that was supposed to do three different things with RSS description text. To make a teaser RSS text, I was asked to develop the functionality in a such a way that text can be cropped in these three ways.

  • with defining the number of words.
  • with defining the numbers of characters.
  • with defining the number of sentences.

Here is a set of functions that helps you crop a text in three different ways. The usage of the function is also mentioned at the end f the script. (more…)

Posted in PHP

5 Comments

How to make sure your PHP RSS feed reader doesn’t mess up

March 18th, 2009 by Fahd

I was recently working on a custom RSS feed reader where the prior most concern of the client was being able to crop the text with respect to the

  1. Number of words.
  2. Numbers of characters.
  3. Number of sentences. (more…)

Posted in PHP

Leave a comment

I am on home page of oDesk, feels great

March 17th, 2009 by Fahd

Hi guys

I am feeling good right now. Have been working on oDesk from Feb 17, 2009. And its good you get paid on hourly basis. I like oDesk the most among freelancing sites I have worked on. Anyhow, I took a few screen shots to show you that I was once featured on home page on oDesk.

(more…)

Posted in PHP, Portfolio, Wordpress

1 Comment

Yahoo Shortcuts

September 9th, 2008 by Fahd

 Enhance your blog posts with Yahoo! Shortcuts.

Enhance your blog posts with Yahoo! Shortcuts.

As you type the content for your blog post, this plugin looks for Flickr Photos, Maps to any locations that you might mention in the post, any products that are listed or even related news to your blogs post.

If your content regularly mentions some companies regularly, Yahoo shortcuts can bring up latest financial information related to that company.

Relevant search results would also come up for certain terms like in the image shown UNIX Pipes would pop up a Yahoo shortcut with relevant contextual search results. Some bloggers may find some shortcuts annoying and irrelevant to their blogs content, but some might really find it useful. It all depends on what you blog about. (more…)

Posted in PHP, Plugins, Wordpress

Leave a comment

Converting comma separated integer/ digit values to an array of integers

September 9th, 2008 by Fahd

OK here is the code to convert a comma separated string of integer/ digit value to an array

$csvintegers='1,2,3,4,7';
$listvals=split(",",$csvintegers);
?>

You can always compare whether if an integer is present in a string or not. The code to do so is (more…)

Posted in PHP

Leave a comment

Convert date formats between PHP and MySQL (mm/dd/yyyy to yyyy-mm-dd)

September 8th, 2008 by Fahd

I have a view in HWC where I am using Javascript DHTML date picker script to pick the date and insert into test field. By the way HWC is the PHP-MVC (using codeigniter) application I am working on.

Date Picker in dhtml/Javascript

Date Picker in dhtml/Javascript

(more…)

Posted in CodeIgniter, PHP

3 Comments

Codeigniter – Clean URLs – Apache mod rewrite : Removing /index.php/ from URL in CodeIgniter Application

August 22nd, 2008 by Fahd

Note: Based on its popularity, updated on 6th june 2011 to clean up code for easy copy paste.

OK readers, here is a simple method to achieve clean urls with your PHP application developed in CodeIgniter.

Please note that this method is applicable only to applications developed in CodeIgniter. Much of the content has been taken from CodeIgniter wiki but rewritten in my own way.

This article explains how to take away “index.php” from your CI application URLs. However, it does NOT remove the need for Index.php, which is the CI front controller i.e. even though Index.php will not appear in the URL, it still needs to be present at the top level of your site (above the /system/ directory). To quote the User Guide,

You can easily remove this file by using a .htaccess file with some simple rules.

You need to perform the following steps to get this working:

  1. Create a .htaccess file to configure the rewrite engine
  2. Set $config['index_page'] to an empty string
  3. Make sure your apache uses the mod_rewrite module
  4. Make sure apache is configured to accept needed .htaccess directives
  5. Restart apache and test

1. Create your .htaccess file

Create a new file named .htaccess and put it in your web directory

RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php

Notes for Windows users:
To create this file you must open Command Prompt and type:

copy con .htaccess [Enter]
[Press CTRL + Z]

A blank .htaccess file will be created. Now you can edit it using Notepad or your favorite text editor and copy the script above.

Note: Most Windows editors will assume that you are attempting to save an .htaccess file as a file with an extension and no filename. The Crimson Editor can be used to create and save .htaccess files and other files that have no filename.

Note: If your site is placed in subfolder specify the path in the “RewriteBase /subfolder/” line.

2. Set $config['index_page'] to an empty string

Open your

system/application/config/config.php

and find the line that assigns $config['index_page'] a value, usually:

$config['index_page'] = &amp;amp;quot;index.php&amp;amp;quot;;

and change it to:

$config['index_page'] = '';

Save the file.

3. Make sure your apache has mod_rewrite activated

This means that the apache must be configured to load the mod_rewrite module (or it might have it compiled-in). For module inclusion, usually you have to look for a line like this in httpd.conf or a file loaded by it (hint: use some quick file search utility to grep files with lines containing ‘rewrite’ string):

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

If you’re running Apache2 type

a2enmod

in the console and when prompted

rewrite

to enable mod_rewrite.

On a Windows machine this line might look this way:

LoadModule rewrite_module modules/mod_rewrite.so

If it is commented out (# in front), make sure to uncomment it and save the file. Checking if the corresponding module exists may be a good idea as well (but it usually does).

Make sure apache accepts needed .htaccess directives

This means that apache is explicitly configured to allow .htaccess files to override those directives that you use in your .htaccess file from step 1. above.

It seems to be sufficient if you add these two lines to your section where you configure the document root for your CI application:

#...
Options FollowSymLinks

AllowOverride FileInfo
#...

There might be other Options listed, just make sure you have FollowSymLinks as well.

Should you get a 500 Internal Server Error, try the following syntax:

Options Indexes Includes FollowSymLinks MultiViews
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all

5. Restart apache and test your application

Works? Congratulations!

Doesn’t work? Ehrrr… well, do not give up; equip yourself with patience, double check all steps above and if it still does not work, post on the forum giving all details of your setup.

How does URL rewriting work?

&amp;amp;lt;IfModule mod_rewrite.c&amp;amp;gt;
...
&amp;amp;lt;/IfModule&amp;amp;gt;

Do what is inside only if Apache has the mod_rewrite feature (by in place compilation, or loaded module).

RewriteEngine On

Activate the URL rewriting engine, if not already done (in main Apache configuration file.

RewriteBase /

Define the part of the URL that won’t change nor be used for rewriting. In fact, this part will be removed before processing, and prepended after processing. This’s a good way to use subfolder-independent rewrite rules. For example, if your CodeIgniter index.php is placed in a virtual host directory, like /tests/, set RewriteBase to /tests/.

RewriteCond %{REQUEST_FILENAME} !-f

Condition to meet for RewriteRule activation. Here, we test if the requested filename does not exist.

RewriteCond %{REQUEST_FILENAME} !-d

Same as above, but we test for directory existence.

RewriteRule ^(.*)$ index.php/$1 [L]

If RewriteCond conditions are met, this rule will be applied. It inserts index.php before the requested URI. The $1 represents the part of string enclosed by parentheses in left expression. The [L] means that this rule is the last one if rule is applied (thus stopping rewriting).

Configuring mod_rewrite in the httpd.conf file

The Apache mod_rewrite docs say

While URL manipulations in per-server context are really fast and efficient, per-directory rewrites are slow and inefficient. If you have access to your httpd.conf file, you’ll have better performance if you configure the rewrite rules in there.

You can add something like this to your httpd.conf:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]

Configuring mod_rewrite and virtual hosting with Apache 2.2

ServerName www.mydomain.com
DocumentRoot /path/to/ci/directory

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Credits: http://codeigniter.com/wiki/mod_rewrite/

Posted in CodeIgniter, PHP

21 Comments

  • 1
  • 2
  • 3
  • 4
  • »
  • Get Posts tagged with

    2008 About Fahd Murtaza Art and Artists Articles beautiful Blogging Blogging Engines cat Databases Entertainment fahd Fahd Murtaza Famous Quotes Flickr flower Flowers funny General google islamabad light lovely macro mansehra markaz murtaza News Open Source Pakistan Pakistani Photographers People Photography PHP PHP Tips picture of the day Plugins Songs Tutorials video Videos Web Development Web Development Software Wordpress You Tube youtube
Fahd Murtaza, Web Developer, Programmer, Wordpress Expert
U
This is portfolio & blog of senior web developer Fahd Murtaza, who has 9 years experience in: website development, WordPress, drupal, CMS and CRM application development with passion of making web better; one site at a time.


@
Mobile +968 93 678 199
email info@fahdmurtaza.com
Google Talk: fahdim@gmail.com
Skype fahd.murtaza
Location Muscat, Oman.


_
Developed using my beloved, love of my life, WordPress, built on the responsive, grid based, mobile optimised, Foundation Framework, and a modified Foundation theme. More Info →


Follow Fahd: Twitter / Google+ / Instagram / Facebook / Dribbble / Tumblr / Posterous/ flickr /
Copyright © 2006-2013 Fahd Murtaza

Skip to toolbar
    • WordPress.org
    • Documentation
    • Support Forums
    • Feedback
Log Out