Forum Moderators: coopster

Message Too Old, No Replies

Remove spaces from all URLs

         

amwd07

10:45 am on Aug 4, 2007 (gmt 0)

10+ Year Member



Hope someone can help me with this really enoying issue 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;
?>

2 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

<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=<?php echo $row_town['twn'];?>"><?php echo $row_town['twn'];?></a></span>
</li>
</ul>
?>

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

darrenG

5:15 pm on Aug 4, 2007 (gmt 0)

10+ Year Member



The two following funtions may be of use to you.

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

If you do not want to use underscores, simply change it to the required character in the code...

amwd07

7:33 pm on Aug 4, 2007 (gmt 0)

10+ Year Member



Thanks for that but I have to say i am a little confused on this one

do I chage anything in the code t owork with my page
I have included functions at the in <head> of the page

do I need anything here to get this to work, it still seems to show spacing?

<ul spry:region="dsTowns" spry:repeatchildren="dsTowns">
<li onclick="" spry:select="selected" spry:hover="hover"><span class="mainlevel2"><a href="dining2.php?twn={twn}">{twn}</a></span>
</li>
</ul>

amwd07

9:17 am on Aug 6, 2007 (gmt 0)

10+ Year Member



please i need help on this on

anybody?

Habtom

9:21 am on Aug 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My guess is your problem could be something else. But the functions provided by darrenG there might be a help to you. You need to pass any URL that you want the space removed to the function:

echo stringForUrl($yoururl);

where you want the URL displayed.

If this doesn't answer your question, you need to be a bit more specific.

amwd07

12:16 pm on Aug 6, 2007 (gmt 0)

10+ Year Member



something I missing here not sure quite what you mean

<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}&amp;twn=<?php echo stringForUrl($strOut);?>{twn}">{twn}</a></span>

I don't think I can have or need php here I can see I need to call the function so i believe this line needs changing

<a href="dining2.php?county={county}&amp;twn= ........ >{twn}</a>

or do I need to call the function before this line if so where & how?

Do i need both fuctions or just this one?
notice changed string out is this correct?

function stringForUrl($strIn) {
$strOut = '{twn}';
$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;
}

Habtom

12:37 pm on Aug 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quesion for you:

Where does the value of {twn} and {country} come from?

amwd07

2:23 pm on Aug 6, 2007 (gmt 0)

10+ Year Member



XML document example
Shropshire Towns www.example.com/new/xml/shropshire-towns.php

var dsTowns = new Spry.Data.XMLDataSet("http://www.example.com/new/xml/{dsCounties::url3}", "export/row", { sortOnLoad: "twn" });

I would like to achieve this also with php
overall I just need SEF urls Much%20Wenlock to become much-wenlock etc.

1){twn} (XML Method)
2)<?php echo towns ["twn"];?> (PHP Method}

[edited by: dreamcatcher at 8:03 am (utc) on Aug. 7, 2007]
[edit reason] Use example.com, thanks. [/edit]

amwd07

9:16 pm on Aug 6, 2007 (gmt 0)

10+ Year Member



Anyone any idea's on this one?

amwd07

1:34 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



Sorry to go on about this but I am almost ready to go live with the site I need the work around for the Search Engine Friendly URLs

Habtom if your there please help?
or anyone else no a way round this one

Habtom

6:28 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can put the function anywhere on the page, running as PHP code but you need to use one of those functions where you want to display the URL

echo stringForUrl($strIn);

$strIn is your URL input.

amwd07

8:57 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



thankyou this works for me now I understand how it works
<?php echo stringForUrl($row_FutureEvents['event_title']);?>

Unfortuntly it does not work with the XML
any idea's

<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}&amp;twn=stringForUrl{twn}">{twn}</a>

</span></div>
</li>
</ul>