How do I remove all places in a string?

Discussion in 'PHP' started by Airwalk Enterprise, Inc, Apr 14, 2011.

  1. #1
    I want to remove all spaces and replace them with -'s from a posted row in mysql database after it is submitted to the form...so that I can use it in a URL without getting %20%, this is what I have...

    $q_title_ = str_replace(" ", "-", "{$_POST['q_title']}");
    $directory = "../question/{$_POST['category']}/$q_title_/";
    mkdir("$directory", 0777);

    any idea what I'm doing wrong?
     
    Airwalk Enterprise, Inc, Apr 14, 2011 IP
  2. sunnyverma1984

    sunnyverma1984 Well-Known Member

    Messages:
    342
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    120
    #2
    try this
    dont use {} on line 2
     
    sunnyverma1984, Apr 15, 2011 IP
  3. EitanXOR

    EitanXOR Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    What sunnyverma1984 suggested won't work, it'll cause a syntax error.

    Your code, other than its formatting, seems okay. Can you be more specific as to what doesn't work exactly? Do you get some error? A blank page? Unexpected result of some kind? A dinasure attack you? Its really hard to say what's wrong when you don't say what's not working.
     
    EitanXOR, Apr 16, 2011 IP
  4. mjx

    mjx Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try this:

    $q_title_ = str_replace(" ", "-", $_POST['q_title']);
    $directory = "../question/".$_POST['category']."/".$q_title_."/";
    mkdir($directory, 0777);
    Code (markup):
     
    mjx, Apr 17, 2011 IP
  5. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #5
    Cool.. trying tweak with wordpress...
     
    Tanya Roberts, Apr 17, 2011 IP
  6. littlejohn199

    littlejohn199 Peon

    Messages:
    42
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try this

    $string = preg_replace('/(\s)\s+/', '-', $string);

    Little John
     
    littlejohn199, Apr 17, 2011 IP