Removing words in a string

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
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Removing words in a string

Post by dominod »

Hi

I want to remove words that end with : within a string.

For example if I write "test: 1234" it will remove "test:" and only echo " 1234"

Can anyone help me with this?

Thanks in advance 8)
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Removing words in a string

Post by Jade »

You need to do a search for semicolons and then replace from the semicolon to the blank space at the beginning of the word or the beginning of the string. Take a look at some of these functions:

http://php.net/manual/en/function.str-replace.php
http://us.php.net/manual/en/function.strrpos.php
http://us.php.net/manual/en/function.strpos.php
http://us.php.net/manual/en/function.strpos.php
http://us.php.net/manual/en/function.strlen.php
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Removing words in a string

Post by Jonah Bron »

Regular expression to the rescue!

Code: Select all

$string = preg_replace('/[^ ]*?:/i', '', $string);
Are you sure you don't want to remove the extra resulting whitespace?
Post Reply