Using String Position and Substring to Extract a Portion of a String using PHP

We’re going to use several string functions together. Let’s take the string testing testing Username:Michele Davis and retrieve only the name.

Using several functions together to extract a portion of a string

$test_string="testing testing Username:Michele Davis";
$position=strpos($test_string,"Username:");

//add on the length of the Username:
$start=$position+strlen("Username:");

echo "$test_string
";
echo "$position
";
echo substr($test_string,$start);
?>

This Code Return

testing testing Username:Michele Davis
16
Michele Davis

Note: The number 16 in our example is the position of our username

Use strpos to search for Username: and return its position in the string with zero being the first position. Use strlen to add on to that position to find where you need to start extracting from the $test_string. To extract the name, use substr, which takes the string as a parameter, returning everything after the $position character in the string.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

No Comments

Checking for a String using PHP

To detect whether a string is part of another string, use strstr. This function takes two parameters: the string to search through and the string to search for. It is not case sensitive; if you want to use a function that is case sensitive, use stristr. Lastly, there is strops, which finds the position of every first occurrence of the string you specified.

Detecting whether a string is contained in another string

$password="secretpassword1";

if (strstr($password,"password")){
echo('Passwords cannot contain the word "password".');
}
else {
echo ("Password accepted.");
}
?>

This Code Returns: Passwords cannot contain the word “password”.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

No Comments

Changing Case (Uppercase to lowercase or inverse) using php

PHP provides functionality for changing the case of a string to all uppercase, all lowercase, or the first letter of a word to uppercase. The commands are strtoupper, strtolower, and ucwords, respectively.

Using the word case functions

$username="John Doe";
echo("$username in upper case is ".strtoupper($username).".
");
echo("$username in lower case is ".strtolower($username).".
");
echo("$username in first letter upper case is ".ucwords($username).".
");
?>

The above code returns:

John Doe in upper case is JOHN DOE.
John Doe in lower case is john doe.
John Doe in first letter upper case is John Doe.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

1 Comment

Calculating the length of a string using PHP

The PHP function strlen can be used to find out how many characters are in a string. This is very useful for validating that there’s data in a string, and that a string isn’t larger than it should be.

$password="secret1";

if (strlen($password) <= 5)
{
echo("Passwords must be a least 5 characters long.");
}
else {
echo ("Password accepted.");
}
?>

Our Password Code will Return: Password accepted

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

No Comments

Cutting up strings or Extract a portion of a string using PHP

The substring functions provide a way to extract a portion of a string. All that’s needed is the string to work with, the position to start from, and how many characters to extract. Use the LEFT, RIGHT, and SUBSTRING functions to do the extraction

LEFT:- Takes the string and the number of characters to extract from the start of the string.
For Example: LEFT(’6128238193′,3)

RIGHT:- Takes the string and the number of characters to extract from the end of the string.
For Example: SUBSTR(’6128238193′,4,3)

SUBSTR:- Takes the string and the number of characters to extract beginning with a certain position in the string.
For Example: RIGHT(’6128238193′, 4)

Adding the formatting to a phone number using LEFT, RIGHT,and SUBSTR

For example: if a database has phone numbers stored in a 10-digit string without any formatting, the numbers could be displayed with the formatting by using the code

SELECT CONCAT('(',LEFT('6128238193',3),')',SUBSTR('6128238193',4,3),'-',RIGHT('6128238193', 4));

These Commands Return: (612)823-8193

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

1 Comment

Auto update your twitter account when you update your wordpress blog

You can update your twitter account update automatically when you update your wordpress blog. It is possible with the help of Tweet-this Plugin.

This plugin can also tweet new blog posts automatically, if you provide
your Twitter credentials in the options. Then a “Send to Twitter” checkbox
appears when writing a new post, along with a text box so you can change the
tweet text for that specific blog post. As of 1.6.1, auto-tweeting works for
scheduled posts and the tweet will be delayed till the post is published.

== Installation ==

  • Upload the `tweet-this` folder to `/wp-content/plugins/`.
  • If you’re using WordPress MU and want this plugin active for all blogs,
    move `tweet-this.php` to `/wp-content/mu-plugins/` at this point.
  • Else, activate the plugin through the ‘Plugins’ menu in WordPress.
  • Tweet This icons should automatically appear on every post and page!
    Go to Settings > Tweet This to change stuff.
  • Optionally, delete readme.txt and the screenshots folder to save space.

Download Plugin Here

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

No Comments

How to Update your WordPress Blog

Update your wordpress old version into current new version

following steps to update your Blog

  • Check Requirements
  • Take a Backup of Your Database
  • Disable Plugins
  • Ready to Update
  • Now that you’ve checked that you’re ready to update, you’ve turned off your plugins and you’ve got your backup, it’s time to get started.

    There are two methods for updating – the easiest is the Automatic Update, which will work for most people. If it doesn’t work, or you just prefer to be more hands-on, you can follow the manual update process.

For More Information Click here

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

No Comments

How to Customize Permalink to make search engine friendly?

Search engines always give importance to URLs, post titles, post introduction area (first Para). So you should customize your URL
With post name. To do that, select ‘Custom structure’ option. Then fill the box with /%postname%/ to make your links as http://sumitbansal.com/upload-a-file-via-ftp-using-php/

This is the best format for any blog. So choose this one and
Click ‘Save settings’. That’s all now you can see your new
Urls in all your blog posts and pages.


Note:
To include the category, you change it to /%category%/%postname%/.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

No Comments

How to Change Permalinks?

1. Login to your wordpress dashboard.
2. Go to ‘Permalinks’ settings option under ‘Settings’ in left navigation.
3. You can see some common settings there. They are
1. Default
2. Date and Name
3. Month and Name
4. Numeric
5. Custom structure

We will see in detail.

For example, consider my blog www.sumitbansal.com

1. In default my post and page urls will be like this
www.sumitbansal.com/?p=1

2. If you choose Date & Name option, urls will be

http://sumitbansal.com/2009/12/22/paging-in-php-splitting-records-into-pages/

3. If you choose Month & Name option urls will be

http://sumitbansal.com/2009/12/paging-in-php-splitting-records-into-pages/

4. If you choose Numeric, it will be like this

http://sumitbansal.com/archives/123

5. The last one is custom structure option. You can use this to
Customize your URL structure.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

No Comments

Paging in PHP (Splitting records into pages)

Paging means showing your query result in multiple pages instead of just put them all in one long page. Imagine waiting for five minutes just to load a search page that shows 1000 result. By splitting the result in multiple pages you can save download time plus you don’t have much scrolling to do.

If there are more records in a table ( say more than 50) then it will not look nice to display all the records in one page and asking visitors to scroll down to see all the records. This will also slow the process of loading of records. So it is better to break all the collected records into different pages with a fix number of records per page ( say ten records per page) .We have to give navigational link at left and right side saying previous and next page. We also have to give links at the center so the visitor can visit in between pages of his/her choice.

To show the result of a query in several pages first you need to know how many rows you have and how many rows per page you want to show. For example if I have 295 rows and I show 30 rows per page that mean I’ll have ten pages (rounded up).

Download Script Click here

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

No Comments

Older Entries »