在PHP中从字符串的开头删除几个字符?

I need to find a specic line of text, from a text-file, and then copy it to a new text-file:

1: I have a text file with several lines of text, eg:

JOHN
MIKE
BEN
*BJAMES
PETE

2: So, I read that text-files contents into an array, with each line of text, placed into a seperate element of the array.

3: Then I tested each element of the array, to find the line that starts with, say: *B ie:

if ( preg_match( "/^\*(B)/",$contents[$a] ) )

Which works ok...

4: Then I copy (WRITE) that line of text, to a new text-file.

Q: So how can I remove the '*B' from that line of text, BEFORE I WRITE it to the new text-file ?

If you already use preg_match, you can modify your regex to get what you want in another variable.

if (preg_match('/^\*B(.*)$/', $contens[$a], $matches)
{
    fwrite($targetPointer, $matches[1]);
}  

After using preg_matchthe variable $matches holds the single matches of subparts of the regex enclosed in brackets. So the relevant part of your line ist matched by (.*) and saved into $matches[1].

Have a try with:

preg_replace('/^\*B/', '', $content[$a], -1, $count);
if ($count) {
    fwrite($file, $content[$a]);
}

This approach writes the lines as the file is read, which is more memory efficient:

$sourceFile      = new SplFileObject('source.txt');
$destinationFile = new SplFileObject('destination.txt', 'w+');
foreach (new RegexIterator($sourceFile, '/^\*B.*/') as $filteredLine) {
    $destinationFile->fwrite(
        substr_replace($filteredLine, '', 0, 2)
    );
}

demo