preg_match question

Discussion in 'PHP' started by hasbehas, Jan 7, 2011.

  1. #1
    I had a "eregi_replace" code before. Now updating the scripts with preg_match..

    I was using this piece of the script to convert "Some Hotel Name.html" to look like "Some-Hotel-Name.html" with the code downbelow at some parts of the page.

    $space = ' ';
    $replace = '/-/';
    $row[hotelname] = preg_replace($replace, $space, $row[hotelname]);
    $hotelname = preg_replace($replace, $space, $hotelname);
    $originalhotelname = preg_replace($space, $replace, $row[hotelname]);

    the var $space is basically a space.. But could not work it out.. what do I have to use as space ? $space ='/ /'; does not work. I get "Some//-//Hote//-//Name.html"
     
    hasbehas, Jan 7, 2011 IP
  2. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #2
    You can try this

    <?php
    $sefurl_separator = '-';// Search engine friendly URL separator
    
    $from = $row['hotelname'];
    $output = preg_replace('/([^a-z0-9\.]+)/i', $sefurl_separator, $from);
    
    echo $output;// This should be >>> Some-Hotel-Name.html
    ?>
    PHP:
     
    xrvel, Jan 7, 2011 IP
    hasbehas likes this.
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    echo str_replace(' ', '-', 'Some Hotel Name.html');
    PHP:
    No need to use regex when its not needed.
     
    danx10, Jan 7, 2011 IP
    hasbehas likes this.
  4. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #4
    problem sorted with ;

    echo str_replace(' ', '-', 'Some Hotel Name.html');

    Thanks all
     
    hasbehas, Jan 7, 2011 IP