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 2 of 2
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts

    how do i use an explode on more than one character?

    Hi,

    i have several strings some have a + in it and some have a - in it and some have a ( or ) in it

    so what i want to try and achieve is to split the string if either of those characters are found

    so i tried this
    PHP Code:
                            $string_array = array("Photo Scanner","Film Scanner","ScanJet Scanner","Flatbed Scanner");
                            
    $match false;
                            foreach(
    $string_array as $v
                            {
                                if(
    preg_match('/'.preg_quote($v).'/i'$prodName)) 
                                {
                                    
    $temp $v;
                                    
    $short_title preg_replace("/$temp/i"""$prodName);
                                    
    $short_title explode(" - "$short_title);
                                    
    $short_title[0] = explode(" + "$short_title[0]);
                                    
    $short_title[0] = explode("("$short_title[0]);
                                    
    $short_title trim($short_title[0]);
                                    break;
                                }
                                else
                                {
                                    
    $short_title $prodName;
                                } 
    but this does not work. if i only use one of the explodes then it works fine, is there a way to get this to work or is there an alternative function i could use?

    many thanks
    Luke

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    If you use preg_split() with /[+()-]/ as the pattern that should do it
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

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