viviosoft Posted April 25, 2011 Share Posted April 25, 2011 Hello all, I want to be able to trim data using an array. For example: I have: REF*19**PHILADELPHIA 02 REF*GG*CUT PILE Which both are separate lines in an array. I want to remove REF*19** and REF*GG* from each and be left with PHILADELPHIA 02 and CUT PILE. I was hoping I could use an array to remove the unwanted data from each line and rebuild the array? Here's what I have currently. Which of course, throws an error. I tried in_array() but maybe I just didn't use it correctly? Thanks for any help you can provide me. <?php $unwantedData = array ('REF*19**', 'REF*GG*' ); $lineArray = array('REF*19**PHILADELPHIA 02', 'REF*GG*CUT PILE'); $newEdiArray = ""; for ($i=0; $i<count($lineArray); $i++) { $line = $lineArray[$i]; $newLine = ltrim( $line, $unwantedData ); $newEdiArray[] = $newLine; } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 25, 2011 Share Posted April 25, 2011 Give this a try function removeRef(&$value) { $value = preg_replace("#^REF\*+[^\*]+\*+#", "", $value); } $lineArray = array('REF*19**PHILADELPHIA 02', 'REF*GG*CUT PILE'); array_walk($lineArray, 'removeRef'); print_r($lineArray); Output: Array ( [0] => PHILADELPHIA 02 [1] => CUT PILE ) NOTE: This will remove the text at the beginning of the value if it matches the following criteria: 1. Begins with "REF" 2. followed by one or more asterisks 3. followed by one or more non asterisks 4. followed by one or more asterisks Quote Link to comment Share on other sites More sharing options...
viviosoft Posted April 25, 2011 Author Share Posted April 25, 2011 Thanks for the quick reply. I should have been a bit more clear. The values that get removed will have different $values. For example: they might not all have "REF" in them so I'm not sure that the solution provided will work. They could be PID*F*MAC*** or MEA**LN* or PID*F*73***, etc. Quote Link to comment Share on other sites More sharing options...
viviosoft Posted April 25, 2011 Author Share Posted April 25, 2011 I ended up using str_replace(); thanks for the help mjdamato! You actually helped get me on the right track. <?php $searchFor = array('REF*19**', 'REF*GG*'); $lineArray = array('REF*19**PHILADELPHIA 02', 'REF*GG*CUT PILE'); $newEdiArray = ""; for ($i=0; $i<count($lineArray); $i++) { $line = $lineArray[$i]; $newLine = str_replace( $searchFor, "", $line ); $newEdiArray[] = $newLine; } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 25, 2011 Share Posted April 25, 2011 This is more efficient. function removeRef(&$value, $key, $searchAry) { $value = str_replace($searchAry, "", $value); } $searchFor = array('REF*19**', 'REF*GG*', 'PID*F*MAC***'); $lineArray = array('REF*19**PHILADELPHIA 02', 'REF*GG*CUT PILE', 'PID*F*MAC***Another name'); array_walk($lineArray, 'removeRef', $searchFor); Quote Link to comment Share on other sites More sharing options...
salathe Posted April 26, 2011 Share Posted April 26, 2011 <?php $searchFor = array('REF*19**', 'REF*GG*'); $lineArray = array('REF*19**PHILADELPHIA 02', 'REF*GG*CUT PILE'); $newEdiArray = ""; for ($i=0; $i<count($lineArray); $i++) { $line = $lineArray[$i]; $newLine = str_replace( $searchFor, "", $line ); $newEdiArray[] = $newLine; } ?> That could be rewritten simply as $searchFor = array('REF*19**', 'REF*GG*'); $lineArray = array('REF*19**PHILADELPHIA 02', 'REF*GG*CUT PILE'); $newEdiArray = str_replace($searchFor, '', $lineArray); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.