我在我的數據庫中獲取了這些數據:如您所見,我在數據庫中存儲了一個 html 模板。這是我用來輸出該 html 的代碼:$php = Blade::compileString($template->content);dd($php) //i used laravel framework, btw.然后這是輸出:<!doctype html><html><head> <meta charset="UTF-8"> <title>Exmaple.com</title> <style> </style></head><body> <div class="account-details"> <b>Account Name</b>: <?php echo e(company.name); ?><br> <b>Plan</b>: <?php echo e(plan.name); ?><br> <b>Source</b>: <?php echo e(source.name); ?><br> <b>Source Type</b>: <?php echo e(source.source_type.name); ?><br> <br> </div> <div class="welcome" style="font-family:Arial;font-size:small"> Hi <?php echo e(user.first_name); ?>,<br> <br> There seems to be a problem blah blah blah<br> <br> Details of the problem are:<br> <?php echo e(sourceMessage); ?><br> <br> You can check and update the source here:<br> https://example.com<br> <br> <br> <span style="font-size:12.8px"> Kind regards,<br> <br> Test Team<br> <br> Email: <a href="mailto:[email protected]" target="_blank">example.com</a><br> Website: <a href="http://example.com" target="_blank">example.com</a><br> </span> </div></body></html>我想在這個例子中將.(dot) 更改為->so,一些文本將輸出如下:從<?php echo e(company.name); ?>到<?php echo e(company->name); ?>從<?php echo e(source.name); ?>到<?php echo e(source->name); ?>所以我認為如果它在 a<?php echo e(whatever); ?>那是我們檢查并替換為的.時間->?我認為這可以通過 RegEx 來完成,但我不是這方面的專家。我想替換它的原因是因為我從電子郵件服務獲取模板然后返回.,所以我想替換它->因為我知道 PHP 讀取->訪問對象而不是..
2 回答

HUWWW
TA貢獻1874條經驗 獲得超12個贊
您可以使用preg_replace查找匹配的代碼片段<?php ... e(f)并將.sf替換為->:
$html = preg_replace_callback('/(<\?php\s+.*?\be\()([^)]+\))/',
function ($m) {
return "{$m[1]}$" . str_replace('.', '->', $m[2]);
},
$html);
請注意,我們使用回調,因為它可以更輕松地處理替換a.b.c為a->b->c. 此外,要真正看起來像 PHP,您需要$在變量名的開頭添加一個,這段代碼就是這樣做的。如果您不想要它,只需更改{$m[1]}$為{$m[1]}

茅侃侃
TA貢獻1842條經驗 獲得超21個贊
如果你在像 VS code 或 sublime 這樣的編輯器上這樣做,你只需要替換 function e usage 部分。
CTRL + H(在 sublime 或 VS 代碼中)打開替換對話框。確保單擊正則表達式選項。
這樣就可以了;
搜索(e\(.+)\.(.+\))
代替$1->$3
- 2 回答
- 0 關注
- 107 瀏覽
添加回答
舉報
0/150
提交
取消