amwd07 Posted August 7, 2007 Share Posted August 7, 2007 Hello I really hope someone can help me with this issue I have posted a message through the webmasterworld forums and they don't seem to be very helpful so I hope things are different here, the forums seem to be an improvement from there site I hope the support is as well Anyway on to my problem ??? I have already tried to rewrite spaced %20 in HTACCESS with no success I am now trying the PHP method preg_replace using example <?php $str = 'foo o'; $str = preg_replace('/\s\s+/', ' ', $str); // This will be 'foo o' now echo $str; ?> two things I would like to achieve here but only to replace url not the actual name 1) XML convert space from {twn} example much%20wenlock needs to be much-wenlock relacing space with - <ul spry:region="dsTowns" spry:repeatchildren="dsTowns"> <?php $str = '{twn}'; $str = preg_replace('/\s\s+/', ' ', $str); // echo $str; <li onclick="" spry:select="selected" spry:hover="hover"><span class="mainlevel2"><a href="dining2.php?twn={twn}">{twn}</a></span> </li> </ul> 2) PHP the same as above but using php recordsets <?php $str = ''; $str = preg_replace('/\s\s+/', ' ', $str); // echo $str; ?> <span class="mainlevel2"><a href="dining2.php?twn=<?php echo $row_town['twn'];?>"><?php echo $row_town['twn'];?></a></span> Or is there a way to replace %20 on the entire site? I know it's not partically damaging to the seaches but it's looks awfull to the visitor The above is probably slightly incorrect or way off course I really hope someone can correct me here as it's driving me crazy and finally this was my reply below appart from the fact no one had explained how to use this function to create search engine friendly URLs, maybe there is an easy method function stringForUrl($strIn) { $strOut = ''; $allowed = '/[^a-zA-Z0-9 ]/'; $strOut = $strIn; // only alphanum and spaces allowed $strOut = preg_replace($allowed, '', $strOut); // swap spaces for _ $strOut = str_replace(' ', '_', $strOut); $strOut = trim($strOut); // make lower case for consistancy $strOut = strtolower($strOut); return $strOut; } function urlForString($strIn) { $strOut = ''; $allowed = '/[^a-zA-Z0-9_ ]/'; $strOut = stripslashes($strIn); // replace all but $allowed $strOut = preg_replace($allowed, '', $strOut); // swap underscores for spaces $strOut = str_replace('_', ' ', $strOut); $strOut = trim($strOut); // lower case for consistency $strOut = strtolower($strOut); return $strOut; } Quote Link to comment https://forums.phpfreaks.com/topic/63746-remove-spaces-from-all-urls/ Share on other sites More sharing options...
effigy Posted August 7, 2007 Share Posted August 7, 2007 Are you trying to convert %20s in the address bar to spaces? Quote Link to comment https://forums.phpfreaks.com/topic/63746-remove-spaces-from-all-urls/#findComment-317664 Share on other sites More sharing options...
amwd07 Posted August 7, 2007 Author Share Posted August 7, 2007 Yes I am trying to convert %20 to either underscore or hypens Quote Link to comment https://forums.phpfreaks.com/topic/63746-remove-spaces-from-all-urls/#findComment-317666 Share on other sites More sharing options...
effigy Posted August 7, 2007 Share Posted August 7, 2007 In actuality or just for display purposes? I'm confused at what needs to happen where. Quote Link to comment https://forums.phpfreaks.com/topic/63746-remove-spaces-from-all-urls/#findComment-317678 Share on other sites More sharing options...
amwd07 Posted August 7, 2007 Author Share Posted August 7, 2007 For example I need URLs like this converting from: http://www.dinewithus.co.uk/new/dining2.php?county=Shropshire&twn=Craven%20Arms to: http://www.dinewithus.co.uk/new/shropshire/craven-arms I can do the first part with Htaccess but need to convert the %20 to - Quote Link to comment https://forums.phpfreaks.com/topic/63746-remove-spaces-from-all-urls/#findComment-317683 Share on other sites More sharing options...
effigy Posted August 7, 2007 Share Posted August 7, 2007 Ah. You want mod_rewrite. Quote Link to comment https://forums.phpfreaks.com/topic/63746-remove-spaces-from-all-urls/#findComment-317695 Share on other sites More sharing options...
amwd07 Posted August 7, 2007 Author Share Posted August 7, 2007 Im am already using mod rewite on the current site below so just need to beable to replace the %20 for - example http://www.dinewithus.co.uk/dining/shropshire-market%20drayton.php market-drayton Quote Link to comment https://forums.phpfreaks.com/topic/63746-remove-spaces-from-all-urls/#findComment-317698 Share on other sites More sharing options...
amwd07 Posted August 7, 2007 Author Share Posted August 7, 2007 Ok maybe this is a better example of what I am trying to achieve here In Dreamweaver CS3 using URL Encode <a href="<?php echo urlencode($row_FutureEvents['event_title']); ?>"><img src="images/events/<?php echo $row_FutureEvents['event_image']; ?>" width="230" height="150" /></a> this does convert the spaces into + but then for some reason this sort of thing appears at the end of the end of the URL %0D%0A this needs to be removed any the + replaces with - http://www.shropshirereview.co.uk/demo/Performance+Cafe%0D%0A I have also tried the following method not sure quite how to get this one to work function stringForUrl($strIn) { $strOut = ' '; $allowed = '/[^a-zA-Z0-9 ]/'; $strOut = $strIn; // only alphanum and spaces allowed $strOut = preg_replace($allowed, '', $strOut); // swap spaces for _ $strOut = str_replace(' ', '_', $strOut); $strOut = trim($strOut); // make lower case for consistancy $strOut = strtolower($strOut); return $strOut; } Quote Link to comment https://forums.phpfreaks.com/topic/63746-remove-spaces-from-all-urls/#findComment-317725 Share on other sites More sharing options...
amwd07 Posted August 7, 2007 Author Share Posted August 7, 2007 Please can anybody help me with this ??? ??? ??? Quote Link to comment https://forums.phpfreaks.com/topic/63746-remove-spaces-from-all-urls/#findComment-317827 Share on other sites More sharing options...
pyrodude Posted August 7, 2007 Share Posted August 7, 2007 You could try using strtr() to "translate" the %20 into - Quote Link to comment https://forums.phpfreaks.com/topic/63746-remove-spaces-from-all-urls/#findComment-317862 Share on other sites More sharing options...
amwd07 Posted August 7, 2007 Author Share Posted August 7, 2007 I have finally had an answer in the webmaster forums this works for me function stringForUrl($strIn) { $strOut = ''; $allowed = '/[^a-zA-Z0-9 ]/'; $strOut = $strIn; // only alphanum and spaces allowed $strOut = preg_replace($allowed, '', $strOut); // swap spaces for _ $strOut = str_replace(' ', '-', $strOut); $strOut = trim($strOut); // make lower case for consistancy $strOut = strtolower($strOut); return $strOut; } <?php echo stringForUrl($row_FutureEvents['event_title']);?> but I can't get this to work with XML if anyone knows how to do that one please help I must be calling the function in the wrong place? or missing brackets? <ul spry:region="dsTowns" spry:repeatchildren="dsTowns"> <li onclick="" spry:select="selected" spry:hover="hover"> <div align="center"><span class="mainlevel2"> <a href="dining2.php?county={county}&twn=stringForUrl{twn}">{twn}</a> </span></div> </li> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/63746-remove-spaces-from-all-urls/#findComment-317871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.