Removing words in a string

Discussion in 'PHP' started by misterdh, Jun 30, 2010.

  1. #1
    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 :cool:
     
    misterdh, Jun 30, 2010 IP
  2. Michellu

    Michellu Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Preg_replace version:
    preg_replace('/([a-zA-Z]*):/','',$str);
    PHP:
    or if you preference the old fashioned, explode way:

    
    $str="test: 1234";
    $final=array();
    $str=explode(' ', $str);
    $n=count($str);
    for ($i=0; $i<$n;$i++)
    	if (strpos($str[$i], ':')===false)
    		$final[]=$str[$i];
    $clean_string=implode(' ', $final);
    echo $clean_string;
    
    PHP:
     
    Michellu, Jun 30, 2010 IP
  3. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks alot! :) :D
     
    misterdh, Jun 30, 2010 IP
  4. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Using this:

    preg_replace('/([a-zA-Z]*):/','',$str);
    PHP:
    How can I make it replace if it is "test:" and if it is "test :" (with a space between the word and semicolon) ?

    Thanks in advance :)
     
    misterdh, Jun 30, 2010 IP
  5. Michellu

    Michellu Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This works with both test: and test :
    preg_replace('/([a-zA-Z]*(\s){0,1}):/','',$str);
    PHP:
     
    Michellu, Jun 30, 2010 IP
  6. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks alot :)

    Just one final question .. How can I make that code inverted? So it removes any word AFTER the semicolons?
     
    misterdh, Jun 30, 2010 IP
  7. sunlcik

    sunlcik Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?php
    /*
    [weilemei.net] (C)2010-2019 weilemei.net Inc.
    This is a freeware

    $Id:test.php 2010-07-01 11:41:07Z weilemei $
    */

    function remove_str($str,$string)
    {
    return str_replace("$str","",$string);
    }
    //e.g
    $string = '123123sdfd12323';
    $str ='fd';
    $return = remove_str($str,$string);

    echo $return;
    ?>

    Holp it can help u.
     
    sunlcik, Jun 30, 2010 IP
  8. sunlcik

    sunlcik Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    <?php
    /*
    [weilemei.net] (C)2010-2019 weilemei.net Inc.
    This is a freeware
    
    $Id:test.php 2010-07-01 11:41:07Z weilemei $
    */
    
    function remove_str($str,$string)
    {
    return str_replace("$str","",$string);
    }
    //e.g
    $string = '123123sdfd12323';
    $str ='fd';
    $return = remove_str($str,$string);
    
    echo $return;
    ?>
    
    
    PHP:
    Holp it can help u.
     
    sunlcik, Jun 30, 2010 IP
  9. technoguy

    technoguy Notable Member

    Messages:
    4,387
    Likes Received:
    308
    Best Answers:
    0
    Trophy Points:
    205
    #9
    You can use str_replace() function to remove particular string from the string, you can replace with blank space and you can remove string
     
    technoguy, Jun 30, 2010 IP