Cutting up strings into seperate strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dave_c00
Forum Commoner
Posts: 37
Joined: Wed May 28, 2003 6:08 am

Cutting up strings into seperate strings

Post by dave_c00 »

Once i have got a string i would like to check the string to see if it has a certain word at the end of it. For example, if the string read in is 124565david. I would like to be able to take the david off the end..

Can anyone help .?

Thanks

Dave
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

Perhaps something like this?

$needle = the string you are looking for
$haystack = the large string that MIGHT contain the needle

Code: Select all

$off_set = strlen($needle);
$whole = strlen($haystack);
$front = $whole - $off_set;

$tail = substr($haystack, $needle, ($off_set * (-1)));
if ($tail == $needle) {
  // $needle was found at the end of the string
  $beg_string = substr($haystack, 0, $front);
} else {
  '' $needle was not found at the end
  $beg_string = $haystack;
}
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Take a look at the Manual.

There is an entire section on string manipulation.

*see my signature for link*
Post Reply