Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-03-2013, 09:05 PM   PM User | #1
milanello72
New Coder

 
Join Date: Feb 2012
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
milanello72 is an unknown quantity at this point
Problem in displaying a URL with dash

How could I display a URL with dash?
I have
Code:
href="/calcioitalia/'.$page.'/'.$id_team.'/'.$name.'.html"
I want to transform calcioitalia/teams/1/Juventus F.C. Torino.html in
calcioitalia/teams/1/Juventus-F.C.-Torino.html

thank you!
milanello72 is offline   Reply With Quote
Old 01-03-2013, 10:24 PM   PM User | #2
tempz
Regular Coder

 
Join Date: Jul 2012
Location: London
Posts: 436
Thanks: 4
Thanked 80 Times in 80 Posts
tempz is an unknown quantity at this point
Quote:
Originally Posted by milanello72 View Post
How could I display a URL with dash?
I have
Code:
href="/calcioitalia/'.$page.'/'.$id_team.'/'.$name.'.html"
I want to transform calcioitalia/teams/1/Juventus F.C. Torino.html in
calcioitalia/teams/1/Juventus-F.C.-Torino.html

thank you!
PHP Code:
$team str_replace("%20""-"$id_team); 
PHP Code:
href="/calcioitalia/'.$page.'/'.$team.'/'.$name.'.html" 
But, if you want a bit more than that, and you probably do, if you are working with URLs, you should take a look at the urlencode() function.

PHP Code:
$team urlencode($id_team

Last edited by tempz; 01-03-2013 at 10:31 PM..
tempz is offline   Reply With Quote
Old 01-03-2013, 10:47 PM   PM User | #3
milanello72
New Coder

 
Join Date: Feb 2012
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
milanello72 is an unknown quantity at this point
thank you for your message!
I foun this function:
Code:
function seoUrl($string) {      
         //Strip any unwanted characters
         $string = preg_replace("/[^A-Za-z0-9_\s-]/", "", $string);
         //Clean multiple dashes or whitespaces
         $string = preg_replace("/[\s-]+/", " ", $string);
         //Convert whitespaces and underscore to dash
         $string = preg_replace("/[\s_]/", "-", $string);
         return $string;
}
I have another problem!
In horizontalmenu.php I will have:
Code:
href="/calcioitalia/'.$page.'/'.$id_team.'/'.$name.'.html"
where $name=seoUrl($name);
*********************************
But how will display teams.php in index.php?? I use
Code:
if (isset($_GET['pagina']) and $_GET['pagina'] == 'teams' and isset($_GET['id_team']) and isset($_GET['name']))
{
         include('./includes/teams.php');
}
but doesn't work because in menuhorizontal i used seoUrl.
I have this message: The requested URL /calcioitalia/teams/2/AC-Milan.php was not found on this server.
How could I solve this problem?
milanello72 is offline   Reply With Quote
Old 01-04-2013, 12:25 AM   PM User | #4
tempz
Regular Coder

 
Join Date: Jul 2012
Location: London
Posts: 436
Thanks: 4
Thanked 80 Times in 80 Posts
tempz is an unknown quantity at this point
Quote:
Originally Posted by milanello72 View Post
thank you for your message!
I foun this function:
Code:
function seoUrl($string) {      
         //Strip any unwanted characters
         $string = preg_replace("/[^A-Za-z0-9_\s-]/", "", $string);
         //Clean multiple dashes or whitespaces
         $string = preg_replace("/[\s-]+/", " ", $string);
         //Convert whitespaces and underscore to dash
         $string = preg_replace("/[\s_]/", "-", $string);
         return $string;
}
I have another problem!
In horizontalmenu.php I will have:
Code:
href="/calcioitalia/'.$page.'/'.$id_team.'/'.$name.'.html"
where $name=seoUrl($name);
*********************************
But how will display teams.php in index.php?? I use
Code:
if (isset($_GET['pagina']) and $_GET['pagina'] == 'teams' and isset($_GET['id_team']) and isset($_GET['name']))
{
         include('./includes/teams.php');
}
but doesn't work because in menuhorizontal i used seoUrl.
I have this message: The requested URL /calcioitalia/teams/2/AC-Milan.php was not found on this server.
How could I solve this problem?
The
PHP Code:
$string 
should be what you're using.. you don't leave it like that. unless you use
PHP Code:
$string 
somewhere.

See what I mean?
tempz is offline   Reply With Quote
Old 01-04-2013, 12:35 AM   PM User | #5
tempz
Regular Coder

 
Join Date: Jul 2012
Location: London
Posts: 436
Thanks: 4
Thanked 80 Times in 80 Posts
tempz is an unknown quantity at this point
PHP Code:
 
preg_replace
(' ''-'$id_team//whitespaces
preg_replace('%20 ''-'$id_team//%20
preg_replace('#\W+#''-'$id_team//description below. 
\W is any non-word character, e.g not A-Z, a-z, 0-9, or underscore (lower case \w is the inverse, so watch your typing).
tempz is offline   Reply With Quote
Old 01-04-2013, 10:49 AM   PM User | #6
milanello72
New Coder

 
Join Date: Feb 2012
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
milanello72 is an unknown quantity at this point
I used
Code:
preg_replace(' ', '-', $name); //whitespaces
preg_replace('%20 ', '-', $name); //%20
preg_replace('#\W+#', '-', $name); //description below. 
       href="/calcioitalia/'.$page.'/'.$id_team.'/'.$name.'.html"
but my url is calcioitalia/teams/1/Juventus F.C. Torino.html
so without '-'

If I use
Code:
href="/calcioitalia/'.$page.'/'.$id_team.'/'.seoUrl($name).'.html"
where
Code:
function seoUrl($string) {
        //Unwanted:  {UPPERCASE} ; / ? : @ & = + $ , . ! ~ * ' ( )
      //  $string = strtolower($string);
        //Strip any unwanted characters
        $string = preg_replace("/[^A-Za-z0-9_\s-]/", "", $string);
        //Clean multiple dashes or whitespaces
        $string = preg_replace("/[\s-]+/", " ", $string);
        //Convert whitespaces and underscore to dash
        $string = preg_replace("/[\s_]/", "-", $string);
        return $string;
}
then I will obtain the link with '-'
calcioitalia/teams/1/Juventus-F.C.-Torino.html
but I have the message
"The requested URL /calcioitalia/teams/1/Juventus-FC-Torino.php was not found on this server."

so the problem is that I must to decode $name in index.php.

Last edited by milanello72; 01-04-2013 at 11:02 AM..
milanello72 is offline   Reply With Quote
Old 01-04-2013, 02:59 PM   PM User | #7
milanello72
New Coder

 
Join Date: Feb 2012
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
milanello72 is an unknown quantity at this point
I tried to add "AllowEncodedSlashes On" in the .htaccess but it didn't work. Any idea?
milanello72 is offline   Reply With Quote
Old 01-04-2013, 03:12 PM   PM User | #8
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
return urlencode($string);
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Old 01-04-2013, 08:43 PM   PM User | #9
milanello72
New Coder

 
Join Date: Feb 2012
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
milanello72 is an unknown quantity at this point
I found at it works!
Code:
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/([a-zA-Z0-9-.\s]+)/?.html$ index.php?page=
$1&id_team=$2&name=$3 [L,QSA]
I put - in ([a-zA-Z0-9-.\s]+)
Thank u for your messages!
milanello72 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:14 PM.


Advertisement
Log in to turn off these ads.