Read First 3 characters and last 4 characters of string

This is a discussion on "Read First 3 characters and last 4 characters of string" within the PHP Forum section. This forum, and the thread "Read First 3 characters and last 4 characters of string are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Apr 27th, 2007, 20:40
JustinStudios's Avatar
Reputable Member
Join Date: Mar 2007
Location: USA
Posts: 404
Blog Entries: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Read First 3 characters and last 4 characters of string

Ok, I am working on something and I need to take a variable (with string information) and look at the first 3 characters and the last 4 characters. I have been baffled as how to do this but I know it can be done (Because everything can be done in PHP!) haha.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Apr 28th, 2007, 01:59
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Read First 3 characters and last 4 characters of string

<?php
$string = "Qwertyuiop";
$toptail = preg_replace('/(...).*(....)/','$1$2',$string);
print $toptail;
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Apr 4th, 2008, 00:01
Junior Member
Join Date: Jan 2008
Location: Reykjavik
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Read First 3 characters and last 4 characters of string

You could also have a look at the strlen() and substr() functions.

PHP: Select all

<?php
$string 
"abcdefgh";

$first substr($string03);    // returns "abc"
$last substr($string, -14);    // returns "efgh"

echo "First 3: ".$first." and the last 4: ".$last;
// First 3: abc and the last 4: efgh
?>
To specify:
substr(string,start,length)

start =
- positive number to start from beginning of the string, negative number to start from end of string.
- The size of the number means from where the count should start.
(3 means start from third character from the beginning of string)

length = how many characters from 'start'

P.s.: I didn't test the code.

Last edited by skuliaxe; Apr 4th, 2008 at 00:10.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
first, last, strings

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting UTF-8 characters to ISO darkling235 JavaScript Forum 2 Mar 23rd, 2008 22:40
Change a string of characters with aonether in a HTML file r3ticul JavaScript Forum 4 Jan 21st, 2008 09:28
Detecting characters between < and > ? Kerosene JavaScript Forum 5 Jan 16th, 2008 16:32
Japanese characters Sheela Starting Out 11 May 12th, 2007 22:20
validate non-ASCII characters brio PHP Forum 1 Feb 7th, 2007 10:08


All times are GMT. The time now is 07:43.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42