PHP question, I hateeeeeeeee. Easy to do i'm sure...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amateurcanada
    Confirmed User
    • Jul 2001
    • 3766

    #1

    PHP question, I hateeeeeeeee. Easy to do i'm sure...

    Hi there! e.g.

    $title="i am the best"

    i want the output field to be "i-am-the-best" doing a replace of spaces with dashes.

    Tell me gods how to do this :D

    be our partner - join nichepartners today
    will.assum.producer @ AmateurCanada.com / icq: 30146166 / facebook.com/will.assum / #amateurcanada
  • Babaganoosh
    ♥♥♥ Likes Hugs ♥♥♥
    • Nov 2001
    • 15841

    #2
    http://us.php.net/manual/en/function.str-replace.php
    I like pie.

    Comment

    • webboy21
      Confirmed User
      • Nov 2004
      • 573

      #3
      ^what he said....easy to do
      Available for: CSS | XHTML | PHP | MySQL | Webdesign

      Comment

      • idogfy
        Registered User
        • Sep 2011
        • 12

        #4
        Copy paste that

        PHP Code:
        <?php
        //try it with 
        $title='  I  can     replace      several   spaces   ';
        //you probably don't want to add a dash at the beginning or at the end of the $title if there is some space at the beginning or at the end
        $title = trim($title);
        // \s+ is the "pattern" you want to replace with dashes
        $new_title = preg_replace('/\s+/','-',$title);
        echo $new_title;
        Have fun with title and dashes
        Last edited by idogfy; 09-21-2011, 05:02 AM.

        Comment

        • islandfreaks
          Registered User
          • Sep 2011
          • 5

          #5
          ^ recommended solution
          Island-Freaks.com - Caribbean Amateurs

          Comment

          • webboy21
            Confirmed User
            • Nov 2004
            • 573

            #6
            yup that one is even better....but if he's putting in the string himself he won't be using double spaces etc......and then the first solution is faster I think...but if he doesn't know what the string will look like..the second solution is superior
            Available for: CSS | XHTML | PHP | MySQL | Webdesign

            Comment

            Working...