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.
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
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?
$safename = preg_replace('/\s\s+/', ' ', $myrow[name]); $safename = str_replace(' ','_',$safename); echo "<a href='$siteurl/$myrow[id]/$safename.html'>$myrow[name]</a>"; Code (markup):
Oh wait, somehow that didn't quite work, it changed all the urls to the first name on the list, not quite sure why?
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) "; $cnt=1; for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ $contents .= " <b>".$i."</b> "; }else{ $contents .= "<a href='$siteurl/album.php?page=$i&id=$id'><font color='#444444'>$i</font></a> "; if($cnt==25) { $contents .= "<br>"; $cnt=0; } $cnt++; } } if(($totalrows % $limit) != 0){ if($i == $page){ $contents .= " <b>".$i."</b> "; }else{ $contents .= "<a href='$siteurl/album.php?page=$i&id=$id'><font color='#444444'>$i</font></a> "; } } include "latestnews.php"; include "random.php"; include "template.php"; ?> Code (markup):
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:
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):
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):
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.
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?