note 127704 added to function.preg-replace
| From: | razvan_bc.yahoo.com via php-notes | Date: | Sat, 08 Oct 2022 10:11:42 +0000 |
| Subject: | note 127704 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219653@lists.php.net to get a copy of this message | ||
How to replace all comments inside code without remove crln = \r\n or cr \r each line?
<?php
$txt_target=<<<t1
this;// dsdsds
nope
/*
ok
*/
is;huge
/*text bla*/
/*bla*/
t1;
/*
=======================================================================
expected result:
=======================================================================
this;
nope
is;huge
=======================================================================
visualizing in a hex viewer .. to_check_with_a_hex_viewer.txt ...
t h i s ; LF TAB n o p e CR LF CR LF i s ; h u g e CR LF
74 68 69 73 3b 0a 09 6e 6f 70 65 0d 0a 0d 0a 69 73 3b 68 75 67 65 0d 0a
I used F3 (viewer + options 3: hex) in mythical TOTAL COMMANDER!
=======================================================================
*/
echo '<hr><pre>';
echo $txt_target;
echo '</pre>';
// a single line '//' comments
$txt_target = preg_replace('![ \t]*//.*[ \t]*!', '', $txt_target);
// /* comment */
$txt_target = preg_replace('/\/\*([^\/]*)\*\/(\s+)/smi', '', $txt_target);
echo '<hr><pre>';
echo $txt_target;
echo '</pre><hr>';
file_put_contents('to_check_with_a_hex_viewer.txt',$txt_target);
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.161 (proxied: 5.14.136.121)
----
Manual Page -- https://php.net/manual/en/function.preg-replace.php
Edit -- https://main.php.net/note/edit/127704
Del: integrated -- https://main.php.net/note/delete/127704/integrated
Del: useless -- https://main.php.net/note/delete/127704/useless
Del: bad code -- https://main.php.net/note/delete/127704/bad+code
Del: spam -- https://main.php.net/note/delete/127704/spam
Del: non-english -- https://main.php.net/note/delete/127704/non-english
Del: in docs -- https://main.php.net/note/delete/127704/in+docs
Del: other reasons-- https://main.php.net/note/delete/127704
Reject -- https://main.php.net/note/reject/127704
Search -- https://main.php.net/manage/user-notes.php