#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    33
    Rep Power
    3

    Please Help with Explode


    Hi
    i've used it before for single words but now I want to split it up like this

    PHP Code:
    <?php 
    $StoreCard 
    "ARC Approved Repair Centre";
    if (
    strpos($StoreCard'Approved Repair Centre')){
    $pieces explode("Approved Repair Centre"$StoreCard);
    $StoreCardDisplayLine1 $pieces[0]; // piece1
    $StoreCardDisplayLine2 $pieces[1]; // piece1
    echo $StoreCardDisplayLine1."<br>";
    echo 
    $StoreCardDisplayLine2."<br>";
    }
    ?>
    it echoes ARC for $StoreCardDisplayLine1
    but $StoreCardDisplayLine2 is blank, supposed to be Approved Repair Centre
    is there a correct way for this method
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    explode() only returns the parts left and right from the separator. So the separator itself isn't included anywhere.

    But I don't even understand what you're trying to do. What's the point of extracting the constant string "Approved Repair Centre"? You could do that by simply using the return value of strpos, though.
  4. #3
  5. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,644
    Rep Power
    1945
    The delimiter will not be part of the result.
    I think you will be better off to use the substr function with the return value from the strpos call.
    Something like this: (untested)
    Code:
    $idxPos = strpos($StoreCard, 'Approved Repair Centre')
    $StoreCardDisplayLine1 = substr($StoreCardidxPos,0,$idxPos)
    $StoreCardDisplayLine2 = substr($StoreCardidxPos,$idxPos)
    It might be possible you have to modify idxPos with +-1 to get the correct result
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    33
    Rep Power
    3
    Ok The Reasons

    User can post Many different values for $StoreCard

    But if perhaps the string contains Approved Repair Centre
    I would like to display it in the next line on my PDF.
    So I need to find Approved Repair Centre and make a variable of it.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    33
    Rep Power
    3
    Originally Posted by MrFujin
    The delimiter will not be part of the result.
    I think you will be better off to use the substr function with the return value from the strpos call.
    Something like this: (untested)
    Code:
    $idxPos = strpos($StoreCard, 'Approved Repair Centre')
    $StoreCardDisplayLine1 = substr($StoreCardidxPos,0,$idxPos)
    $StoreCardDisplayLine2 = substr($StoreCardidxPos,$idxPos)
    It might be possible you have to modify idxPos with +-1 to get the correct result
    Can you make this work
    I've tried but giving me errors or no results
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    33
    Rep Power
    3
    Cool
    Here is the Solution
    PHP Code:
    <?php 
    $StoreCard 
    "ARC Approved Repair Centre";

    if (
    strpos($StoreCard'Approved Repair Centre')){
    $StoreCard str_replace("Approved",'_split_Approved'$StoreCard);
    $pieces explode("_split_"$StoreCard);
    $StoreCardDisplayLine1 $pieces[0]; // piece1
    $StoreCardDisplayLine2 $pieces[1]; // piece2
    echo $StoreCardDisplayLine1."<br>";
    echo 
    $StoreCardDisplayLine2."<br>";
    }
    ?>
    I had to add the separator like Jacques1 said to make explode work correctly, thanks
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by jpmul
    But if perhaps the string contains Approved Repair Centre
    I would like to display it in the next line on my PDF.
    So I need to find Approved Repair Centre and make a variable of it.
    Is it possible that there's something after 'Approved Repair Centre'? Because if it's always just 'Approved Repair Centre', then simply take the part before this string and the string itself, and you're done.
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by jpmul
    Cool
    Here is the Solution
    [...]
    I had to add the separator like Jacques1 said to make explode work correctly, thanks
    No, that's not a good solution. When you have to change your string to make the function work, it's obviously the wrong function.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    33
    Rep Power
    3
    No there will never be words after Approved Repair Centre
    This works OK for me
    Below is another version if it is only Approved Repair Centre
    PHP Code:
    <?php 
    $StoreCard 
    "Sovereign ARC Approved Repair Centre";

    if (
    strpos($StoreCard'Approved Repair Centre')){
    $StoreCard str_replace("Approved",'_split_Approved'$StoreCard);
    $pieces explode("_split_"$StoreCard);
    $StoreCardDisplayLine1 $pieces[0]; // piece1
    $StoreCardDisplayLine2 $pieces[1]; // piece1
    }

    if (
    $StoreCard == "Approved Repair Centre"){$StoreCardDisplayLine1 $StoreCard$StoreCardDisplayLine2 ""; }    

    echo 
    "Part 1 = ".$StoreCardDisplayLine1."<br>";
    echo 
    "Part 2 = ".$StoreCardDisplayLine2."<br>";
    ?>
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    You really love explode(), do you?

    PHP Code:
    <?php

    $StoreCard 
    "Sovereign ARC Approved Repair Centre"

    if (
    $pos strpos($StoreCard'Approved Repair Centre')){  // yeah, the "=" is intentional
        
    $prefix substr($StoreCard0$pos);
        echo 
    'Part 1 = ' htmlentities($prefix) . '<br />';
        echo 
    'Part 2 = Approved Repair Centre<br />';
    }

    Comments on this post

    • MrFujin agrees : for a workable code using substr; I somehow misspelled the variable name.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    33
    Rep Power
    3
    I need variables for part one and part two
    I print it in a pdf.
    I'm ok with my method for now. Thanks Jacques

IMN logo majestic logo threadwatch logo seochat tools logo