Jump to content

Recommended Posts

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; 
} 

Link to comment
https://forums.phpfreaks.com/topic/63746-remove-spaces-from-all-urls/
Share on other sites

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 -

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; 
} 

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> 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.