Need help in PHP string replace function

Discussion in 'PHP' started by kks_krishna, May 15, 2007.

  1. #1
    HI,

    I have string "test 3.0 krish"

    in the above string i want to replace space and .(dot) to "-". so that result should appear as : test-3-0-krishna.

    is there any function to achiveve this.
     
    kks_krishna, May 15, 2007 IP
  2. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #2
    $temp="test 3.0 krish";
    $temp=str_replace(" ","-",$temp);
    $temp=str_replace(".","-",$temp);
     
    YIAM, May 15, 2007 IP
  3. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #3
    when i have multiple spaces in the string like : $temp="test 3.0 krish";
    how to trim the spaces and put only one "-"
     
    kks_krishna, May 15, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    
    function format_string( $string )
    {
    	$string = preg_replace( "@ +@", " ", $string );
    	return str_replace(array(' ', '.'), '-', $string );
    }
    
    echo format_string( 'a normal string' );
    echo '<br />';
    echo format_string( 'a  not  normal.string' );
    
    PHP:
     
    krakjoe, May 15, 2007 IP
  5. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #5
    hi krakjoe, great code man.i like the way you made the function.i will use it.
     
    coderbari, May 15, 2007 IP
  6. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #6
    thanks a lot friend
     
    kks_krishna, May 15, 2007 IP
  7. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #7
    hi,

    how to reterive the value from session object?
     
    kks_krishna, May 15, 2007 IP
  8. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #8
    $session_object=$_SESSION['object'];
     
    YIAM, May 15, 2007 IP