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.

php preg_replace help please

Discussion in 'PHP' started by JEET, May 13, 2015.

  1. #1
    Hi,
    I got a string like this:


    some text Some text 2.9 3.00 1.2 300.20 4444.666 100000.3333441


    I want to remove the decimal numbers from this string without using explode() and then checking each value fwith is_numeric()
    I think it'll be better if I can use preg_replace. Please note that the string can also be like this:


    some text 400 Some text 2.9 3.00 1.2 300.20 4444.666 100000.3333441


    I don't want to remove the number between the text, just the ones at the end of the text.

    What will be the preg_replace pattern?
    Thanks
     
    JEET, May 13, 2015 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    What would the result be in both cases? Do you just want to remove decimals and leave integers alone?
     
    ThePHPMaster, May 13, 2015 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    What exactly would you be replacing it WITH?

    The question doesn't make a lot of sense, at least not without some idea what you want for output and what you want to do with that output.
     
    deathshadow, May 14, 2015 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    Never mind guys! I did this, but without using preg.
    deathshadow, I just wanted to remove those, not replace. So I would have used a empty "" in the preg_replace...
    I like your signature. So true!!!

    I deleted the script which did the job, but here's how it went:

    The string was exploded using white space. $array = explode(' ',$str);
    Each element was checked if it was numeric and had a '.' sign using strpos If it did, the array element was unset.
    Then finally an implode. $final= implode(' ',$array);

    Thanks :)
     
    JEET, May 15, 2015 IP
  5. Sano000

    Sano000 Active Member

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    5
    Trophy Points:
    53
    #5
    preg_replace was a good idea. You need to leave a text only, right? So here is javascript example, but regex will be the same for PHP:
    'some text 400.33 Some text 2.9 3.00 1.2 300.20 4444.666 100000.3333441'.replace(/[\.\s\d]+$/, '')
    
    Code (markup):
    This regex means to remove all ([]+) dots (\.), spaces (\s) and numbers (\s) from the end of the string ($)
     
    Sano000, May 16, 2015 IP
    ThePHPMaster likes this.
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Wrong, If I interpreted the question right (hard with the ranguage rarrier) the 400 in the middle needed to stay. Basically they want to only strip them from the end... and I believe only if is_float.
     
    deathshadow, May 16, 2015 IP
  7. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #7
    Hi JEET.
    $strIn = 'some text 400 Some text 2.9 3.00 1.2 300.20 4444.666 100000.3333441';
    $strOut = preg_replace('/(\d+)\.\d+/', '$1', $strIn);
    echo $strOut;
    PHP:
    Output:
    some text 400 Some text 2 3 1 300 4444 100000
    Code (markup):
    Is this what you need?
     
    2WDH.com, May 21, 2015 IP