Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,088
    Thanks
    296
    Thanked 12 Times in 12 Posts

    Exclamation help splitting a string

    Hi All,

    i am looking for some help please, im trying to split a string where a "-" is present and use everything after that and discard everything before

    for example if i had

    my first record - its my life

    i want to grab just "its my life"

    can anyone help please
    many thanks
    Luke

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Easiest way is just an explode:
    PHP Code:
    $aPieces explode('-'$string);
    print 
    $aPieces[1]; 
    You'll need to confirm that the - exists with either a count (returns the number of pieces, 1 if no - present, two pieces if 1 - is present, etc).
    There are other approaches of course, you can substring it based on a - itself. Personally, I'm lazy
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    LJackson (02-04-2010)

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,088
    Thanks
    296
    Thanked 12 Times in 12 Posts
    Hi mate,

    thanks for your help i appreciate it

    i ended up chosing explode after looking at some alternatives and have the following code

    PHP Code:
    $keywords preg_replace("/:|\s*\(.*\)|\s*\[.*\]/U"""$keywords);
    $keywords_short explode("-"$keywords);
    $keywords_short $keywords_short[1]; 
    but when i print out $keywords_short; nothing prints out?
    the $keywords has an - in it

    any ideas
    cheers
    Luke
    Last edited by LJackson; 02-04-2010 at 12:02 AM.

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,088
    Thanks
    296
    Thanked 12 Times in 12 Posts
    its ok solved it, relised that i had a sting replace function removing all - on another function thats why it wasnt working

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Just a note...
    If you used this code:
    PHP Code:
    $keywords "test - some text - the rest of the text";
    $keywords_short explode("-"$keywords);
    $keywords_short $keywords_short[1];
    echo 
    $keywords_short
    The text:
    some text
    Would be echoed because your array looks like this:
    Code:
    Array
    (
        [0] => 'test '
        [1] => ' some text '
        [2] => ' the rest of the text'
    )
    In order to echo everything after the first hyphen you need to do something like this:
    PHP Code:
    $keywords "test - some text - the rest of the text";
    $keywords_short explode("-"$keywords);
    $count count($keywords_short);
    for(
    $i=1;$i<$count;$i++){
    $keywords_short .= $keywords_short[$i];
    $keywords_short $keywords_short[1];
    }
    echo 
    $keywords_short
    -Sam

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    There are many ways to do this thing. Using explode() is a good way. Also you could do it using String functions:
    PHP Code:
    <?php
    $keywords 
    "test - some text - the rest of the text";
    $position strrpos($keywords,'-'); // position of the last occurrence 
    echo (false===$position)?$keywords:substr($keywords,$position+1);
    ?>
    Probably the worst way would be to use regular expressions because regular expression functions are slower and take more resources.


  •  

    Posting Permissions

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