remove spaces preg replace

Discussion in 'PHP' started by Astroman, Apr 16, 2008.

  1. #1
    I think you can use preg replace to remove spaces if they occur with php but I can't remember how it works?

    I have this code to make my URLS for instance:

    echo "<a href='$siteurl/$myrow[id]/$myrow[name].html'>$myrow[name]</a>";
    Code (markup):
    and some of the 'names' have two words with a space so I want to make sure the space is removed, or replaced with a '_' in the href part.

    Can someone tell me how I can alter the above to remove any spaces in the URL?

    Thanks. :)
     
    Astroman, Apr 16, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You can use:

    $str = preg_replace('/\s\s+/', ' ', $str);
    PHP:
    To trim excess whitesplace, and www.php.net/str_replace to replace spaces to underscores.

    Jay
     
    jayshah, Apr 16, 2008 IP
    Astroman likes this.
  3. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #3
    Nice code shah
     
    m0nkeymafia, Apr 16, 2008 IP
  4. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Jeepers that was fast. :)

    Can I be a PHP numptie and ask how that sits within the code I posted, I haven't a clue?
     
    Astroman, Apr 16, 2008 IP
  5. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #5
    
    $safename = preg_replace('/\s\s+/', ' ', $myrow[name]);
    $safename = str_replace(' ','_',$safename);
    
    echo "<a href='$siteurl/$myrow[id]/$safename.html'>$myrow[name]</a>";
    
    Code (markup):
     
    m0nkeymafia, Apr 16, 2008 IP
    Astroman likes this.
  6. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #6
    Cool, thanks guys, rep added to both. :)
     
    Astroman, Apr 16, 2008 IP
  7. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #7
    Oh wait, somehow that didn't quite work, it changed all the urls to the first name on the list, not quite sure why?
     
    Astroman, Apr 16, 2008 IP
  8. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #8
    Show us the inputs / outputs dude :)
     
    m0nkeymafia, Apr 16, 2008 IP
  9. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #9
    Oddly enough it's for a site very similar to the one in your sig:

    http://www.celebritywalls.net

    This is the unchanged code for the list at the bottom I think:
    
    <?php
    include "squidhbcne.php";
    
      $query  = "SELECT * FROM celebrity  ORDER by name asc"; 
      $result = mysql_query($query,$db) or die("Error: " . mysql_error()); 
      $myrow = mysql_fetch_array($result);
      $totalrows = mysql_numrows($result);
      $percol = $totalrows/3 + 1;  
      
      
    
    echo "<table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse; color: #DDDDDD' bordercolor='#000000' width='99%' id='AutoNumber4'><tr><td bgcolor ='#F2F2F2' colspan = '3'><b><font face='Tahoma' color='#0000ff'>List of celebrities</font></b></td></tr><tr><td>
    <div  style='width:640px;height:200px;background-color:white;overflow:auto'>
    <table width ='100%'><tr>
    ";
    $ctr = 1;
    
           do {  
    if($ctr==1) echo "<td width='33%' valign='top'>";
    
    echo "<a href='$siteurl/$myrow[id]/$myrow[name].html'><font size='2' face='tahoma' color='#000000'>$myrow[name]</b> </a><br>";
    
    if($ctr>$percol){
       echo "</td>";
       $ctr=0;
                      }
    $ctr++;
    
             } while ($myrow = mysql_fetch_array($result));
    if($ctr > 1) echo "</td>";
    echo "</tr></table></div></td></tr></table>";
    
    ?>
    Code (markup):
    And I think this page also makes urls for the pictures:

    <?php
    
    include "squidhbcne.ph";
    include "function.php";
    
    $page = $_GET[page];
    $id = $_GET[id];
    
    
    //save points
    
    $records = mysql_query("select * from celebrity where id = '$id'");
    $dta = mysql_fetch_array($records);
    $points = $dta[points];
    $points++;
    
    mysql_query("UPDATE celebrity SET points = '$points' WHERE id = '$id'");
    
    
    $name = $_GET[name];
    
    $limit = 12;                 
       if(empty($page)){
           $page = 1;
         }
    
    
    $sq ="SELECT * FROM photos WHERE celebid = '$id'";
    $rst = mysql_query($sq) or die (mysql_error());
    $totalrows = mysql_numrows($rst);
    
    $limitvalue = $page * $limit - ($limit);
    
    
    $pagetitle = "$name Photo galery";
    $heading = "$name Photo galery";
    
    
    $sql = "SELECT * FROM photos WHERE celebid = '$id' LIMIT $limitvalue,$limit";
    $rec = mysql_query($sql) or die (mysql_error());
    $datas = mysql_fetch_array($rec);
    
    
    $contents .= "<table width ='95%'  border='0' cellpadding='2' cellspacing='0'>";
    
    $ctr = 1;
    
    
    do{
      
       $location=trim($datas[location]);
       $pics = "$siteurl/pictures/$location"; 
       $iz = @getimagesize($pics);
        $pics = str_replace(" ","%20",$pics);
    
    $property = @imageResize($iz[0], $iz[1], 145);   
    $pics = "<img src=\"$pics\" $property border = '1' style ='border: 1px solid #111111; padding: 2' alt = '$name picture'>";
    
    if($ctr==1) $contents .= "<tr>";
    
    $contents .= "<td align='center'><a href = '$siteurl/zoom/$location/$name/' title = '$name picture'>$pics</a></td>";
    
    
    if($ctr==4){
    $a++;
    if($a==1){
    $contents .= "</tr><tr><td colspan='4'>
    
    </td>";
             }
    
     $ctr=0;
     $contents .= "</tr>";
            }
    $ctr++;
    
            
    } while($datas = mysql_fetch_array($rec)); 
    
    
    if ($ctr==2) $contents .= "<td></td><td></td><td></td></tr>";
    if ($ctr==3) $contents .= "<td></td><td></td></tr>";
    if ($ctr==4) $contents .= "<td></td></tr>";
    $contents .= "</table>";
    
    
    $numofpages = $totalrows / $limit;
    $contents .= "<font size='1' face='verdana'><br>Page(s) &nbsp;"; 
     
     
    $cnt=1;
        for($i = 1; $i <= $numofpages; $i++){
            if($i == $page){
                $contents .= "&nbsp;<b>".$i."</b>&nbsp;";
            }else{ $contents .= "<a href='$siteurl/album.php?page=$i&id=$id'><font color='#444444'>$i</font></a>&nbsp;";
            if($cnt==25) {
                      $contents .= "<br>";
                       $cnt=0;
                     }
                  $cnt++;
            }
    
        }
    
        if(($totalrows % $limit) != 0){
            if($i == $page){
                $contents .= "&nbsp;<b>".$i."</b>&nbsp;";
            }else{
                $contents .= "<a href='$siteurl/album.php?page=$i&id=$id'><font color='#444444'>$i</font></a>&nbsp;";
            }
        }
    
    include "latestnews.php";
    include "random.php";
    
    
    
    
    
    include "template.php";
    
    ?>
    
    
    
    Code (markup):
     
    Astroman, Apr 16, 2008 IP
  10. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #10
    Sorry dude, just show me the bit of code where you put the ereg_replace, and show me what it output
    
    That code you pasted doesnt have the changes in?
    HTML:
     
    m0nkeymafia, Apr 16, 2008 IP
  11. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #11
    You can see the List of Celebrities here: http://www.celebritywalls.net at the bottom with the altered file code:

    <?php
    include "squidhbcne.php";
    
      $query  = "SELECT * FROM celebrity  ORDER by name asc"; 
      $result = mysql_query($query,$db) or die("Error: " . mysql_error()); 
      $myrow = mysql_fetch_array($result);
      $totalrows = mysql_numrows($result);
      $percol = $totalrows/3 + 1;  
      $safename = preg_replace('/\s\s+/', ' ', $myrow[name]);
    $safename = str_replace(' ','_',$safename);
      
    
    echo "<table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse; color: #DDDDDD' bordercolor='#000000' width='99%' id='AutoNumber4'><tr><td bgcolor ='#F2F2F2' colspan = '3'><b><font face='Tahoma' color='#0000ff'>List of celebrities</font></b></td></tr><tr><td>
    <div  style='width:640px;height:200px;background-color:white;overflow:auto'>
    <table width ='100%'><tr>
    ";
    $ctr = 1;
    
           do {  
    if($ctr==1) echo "<td width='33%' valign='top'>";
    
    echo "<a href='$siteurl/$myrow[id]/$safename.html'>$myrow[name]</a><br>";
    
    if($ctr>$percol){
       echo "</td>";
       $ctr=0;
                      }
    $ctr++;
    
             } while ($myrow = mysql_fetch_array($result));
    if($ctr > 1) echo "</td>";
    echo "</tr></table></div></td></tr></table>";
    
    ?>
    Code (markup):
     
    Astroman, Apr 16, 2008 IP
  12. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #12
    You have put the whitespace stripping code in the wrong place, try this:

    
    
    <?php
    include "squidhbcne.php";
    
      $query  = "SELECT * FROM celebrity  ORDER by name asc"; 
      $result = mysql_query($query,$db) or die("Error: " . mysql_error()); 
      $myrow = mysql_fetch_array($result);
      $totalrows = mysql_numrows($result);
      $percol = $totalrows/3 + 1;  
    //the code was here
    
    echo "<table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse; color: #DDDDDD' bordercolor='#000000' width='99%' id='AutoNumber4'><tr><td bgcolor ='#F2F2F2' colspan = '3'><b><font face='Tahoma' color='#0000ff'>List of celebrities</font></b></td></tr><tr><td>
    <div  style='width:640px;height:200px;background-color:white;overflow:auto'>
    <table width ='100%'><tr>
    ";
    $ctr = 1;
    
           do {  
    if($ctr==1) echo "<td width='33%' valign='top'>";
    
    //The code is now here
      $safename = preg_replace('/\s\s+/', ' ', $myrow[name]);
    $safename = str_replace(' ','_',$safename);
    echo "<a href='$siteurl/$myrow[id]/$safename.html'>$myrow[name]</a><br>";
    
    if($ctr>$percol){
       echo "</td>";
       $ctr=0;
                      }
    $ctr++;
    
             } while ($myrow = mysql_fetch_array($result));
    if($ctr > 1) echo "</td>";
    echo "</tr></table></div></td></tr></table>";
    
    ?>
    
    
    Code (markup):
     
    m0nkeymafia, Apr 16, 2008 IP
  13. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #13
    Thanks, that works. I have to figure out how to make the picture links do the same now, and stop it putting underscores in the titles which it now does if you click the list on links at the bottom. :)
     
    Astroman, Apr 16, 2008 IP
  14. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #14
    Okay I managed to get all the spaces removed everywhere I think, only now there's no spaces in the picture titles on the page either:

    http://www.celebritywalls.net/zoom/jenniferlovehewitt1.jpeg/Jennifer_Love_Hewitt/

    The software is a bit rubbish I think, but maybe I can use it to learn how to make it better and a few snippets of code etc?

    Will I have to make a different function to create the titles do you think?
     
    Astroman, Apr 16, 2008 IP