Need help for creating URL using PHP Reg Exp

Discussion in 'PHP' started by kks_krishna, Jul 7, 2007.

  1. #1
    HI,

    I want to create urls dynamically when i am inserting the new data to database.

    ForExample,

    If the story title is :

    What is new in DP?

    It should be converted as :

    what-is-new-in-dp.html

    It shouldn't allow any special characters in the URL.

    How can I do this?
     
    kks_krishna, Jul 7, 2007 IP
  2. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #2
    
    <?php
    	$title = "what's new in DP?";
    	$title = preg_replace("/[^a-z0-9-]/","",strtolower(str_replace(" ","-",$title)));
    	echo $title.".html";
    ?>
    
    PHP:
     
    ansi, Jul 7, 2007 IP
  3. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Its working. But the URL is ending the "-".

    for example :

    test-app-.html.

    i want to remove the "-" at end of the url.

    how can i do that?
     
    kks_krishna, Jul 10, 2007 IP
  4. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #4
    
    <?php
        $title = "what's new in DP?";
        $title = preg_replace("/[^a-z0-9-]/","",strtolower(str_replace(" ","-",$title)));
        if ( $title[strlen($title)-1] == "-" )
            $title = substr($title,0,strlen($title)-1);
        echo $title.".html";
    ?>
    
    PHP:
     
    ansi, Jul 10, 2007 IP