SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    NH
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Remove charater between / and +

    Hi,

    I am trying to remove the words between / and +

    Example:
    =ft myers / sw florida+

    Desired output would be
    =ft myers+

    Another example
    =flagstaff / area+

    Desired output would be
    =flagstaff+

    I think I need a trim function but cant get it to work.

    Thanks for any feedback.

    Nate

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use php string manipulation functions http://nz.php.net/strings have a look at strpos() and substr()

    Or you could use regular expressions http://nz2.php.net/manual/en/ref.pcre.php

  3. #3
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    $myString='=ft myers / sw florida+';
     
    $myString = preg_replace("/\h*\/.+/","+",$myString);
    echo $myString;
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    NH
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both for your replies...ended up using the code Glen supplied and it work great.

    Thanks again,

    Nate

  5. #5
    SitePoint Member
    Join Date
    May 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    its always advisable to use templates but its better to create own method rather than posting on web forms for help. Using simple arrays will solve this prob very easily but coders want short cuts even in easier things this only reduces coding capability nothing more. Thats not advisable.

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    Bear in mind that glenngould's code would truncate strings containing a forward slash without a suffixed plus symbol.

    For example:
    PHP Code:
    $myString='=ft myers / sw florida';
     
    $myString preg_replace("/\h*\/.+/","+",$myString);
    echo 
    $myString// =ft myers+ 
    A more explicit pattern which does not have this problem is ~\h*/[^+]*+\+~ which for the above code would end up echoing =ft myers / sw florida as expected.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •