我正在尝试@
从文本块中删除标志。问题是在某些情况下(在一行的开头,@
标志需要保留。
我已经通过使用 RegEx 模式成功了.\@
,但是当@
符号确实被删除时,它也会删除它前面的字符。
目标:删除所有@
符号,除非@
符号是该行中的第一个字符。
<?php
function cleanFile($text)
{
$pattern = '/.\@/';
$replacement = '%40';
$val = preg_replace($pattern, $replacement, $text);
$text = $val;
return $text;
};
$text = ' Test: test@test.com'."\n";
$text .= '@Test: Leave the leading at sign alone'."\n";
$text .= '@Test: test@test.com'."\n";
$valResult = cleanFile($text);
echo $valResult;
?>
输出:
Test: tes%40test.com
@Test: Leave the leading at sign alone
@Test: tes%40test.com