1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Preg Replace / Trim

Discussion in 'PHP' started by GEN!US, Sep 19, 2005.

  1. #1
    hey guys,
    I need help with a few lines of code...

    i want to change
    "Digital_Point.Forums-2005-Rocks" to "Digital Point Forums"

    What i need is to know how to:
    - replace dots and underscores with <SPACE>
    - ignore all text after the last hyphen
    - ignore certain phrases e.g 2005

    "Digital_Point.Forums-2005-Rocks" /* original string */
    "Digital Point Forums 2005-Rocks" /* string after underscores and dots replaced */
    "Digital Point Forums 2005" /* last hypen and text after removed */
    "Digital Point Forums" /* final string */

    I'd appreciate such help... thanks in advance
     
    GEN!US, Sep 19, 2005 IP
  2. draculus

    draculus Peon

    Messages:
    63
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What you need is the str_replace() function. pass three parameters, the first is the text you want to replace, the second is the text you want to replace it with and the last is the original text.

    e.g.

    $myString = str_replace("-", " ", $myString);

    will replace all hyphens with a space.

    $myString = str_replace("_", " ", $myString);

    does the same for underscores

    $myString = str_replace("2005", "", $myString);

    will remove the text "2005" from the string.
     
    draculus, Sep 19, 2005 IP
  3. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #3
    If $string is the string you want to clean up, this will do it:
    str_replace (array ('.', '_'), ' ', substr ($string, 0, strpos ($string, '-')))
    PHP:
     
    digitalpoint, Sep 19, 2005 IP
  4. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Wouldn't it be:
    $string = str_replace (array ('.', '_', '-', '2005', '  '), ' ', substr ($string, 0, strrpos ($string, '-')));
    PHP:
    ?
     
    exam, Sep 19, 2005 IP
  5. GEN!US

    GEN!US Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks... that draculus... that all works...

    what about the part where i need to replace/delete all text between brackets [ ] ( ) {}

    i did some research on it... but tried and still not working
     
    GEN!US, Sep 20, 2005 IP
  6. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #6
    Not necessary because I'm killing anything after the first dash.
     
    digitalpoint, Sep 20, 2005 IP
  7. draculus

    draculus Peon

    Messages:
    63
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sorry didn't see anything about brackets in the original question. Erm... still don't.
     
    draculus, Sep 20, 2005 IP
  8. GEN!US

    GEN!US Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    its ok... i figured it out ;) thanks for the prior help... the code is below

    $start = strpos($term, "["); // find position of first bracket
    $end = strpos($term, "]"); // find position of second bracket
    $length = strlen($term); // length of word
    $prefix = substr($term, 0, $start-1); // store all text before first bracket in prefix
    $suffix = substr($term, $end+1, $length); // text after equal suffix
    $term = $prefix;
    $term .= $suffix; // merge the two

    maybe it could have been simplified... but i'm new to php so hush! :p
     
    GEN!US, Sep 20, 2005 IP
  9. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Oh, I though he wanted to kill anything after the last dash. My bad.
     
    exam, Sep 21, 2005 IP