Page 1 of 1

Removing words in a string

Posted: Wed Jun 30, 2010 7:19 am
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)

Re: Removing words in a string

Posted: Wed Jun 30, 2010 7:40 am
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

Re: Removing words in a string

Posted: Wed Jun 30, 2010 12:35 pm
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?