i'm a newbie and i have a text file.. the contents of the text file are as follows...
text1 text4 text7
text2 text5 text8
text3 text6 text9
what i want to do is add this --->>>
character to every line in the first two vertical columns of the text file using php
... how can i do this.... any help would be appreciated... thanks in advance... :).. i have tried the following code though...
<?php
$fileContents = file_get_contents('mytext.txt');
$fixedFileContents = "--->>>";
file_put_contents($fixedFileContents, 'mytext.txt');
?>
the output should look something like
--->>>text1 --->>>text4 text7
--->>>text2 --->>>text5 text8
--->>>text3 --->>>text6 text9
If I understand your question correctly, you can use preg_replace
and regex for this:
$fileContents = preg_replace('/^(\w+\s+)(\w+\s+)/m', '--->>>$1--->>>$2', $fileContents);
Example:
<?php
$fileContents = <<<TEXT
text1 text4 text7
text2 text5 text8
text3 text6 text9
TEXT;
$fileContents = preg_replace('/^(\w+\s+)(\w+\s+)/m', '--->>>$1--->>>$2', $fileContents);
echo $fileContents;
?>
Output:
--->>>text1 --->>>text4 text7
--->>>text2 --->>>text5 text8
--->>>text3 --->>>text6 text9
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments