Jump to content

ltrim and array


viviosoft

Recommended Posts

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;
}

?>

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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;
}

?>

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

<?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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.