Hướng dẫn dùng back reference trong PHP
Cú pháp hàm preg_replace()Hàm Show Nội dung chính
preg_replace($pattern, $replacement,$subject) Hàm này tìm trong
$replacement đặc biệt có thể được biểu diễn một
cách đặc biệt, nó có thể chứa các tham chiếu dùng để chèn nội dung, tham chiếu có dạng Hàm Ví dụ 1 về preg_replaceDùng biểu thức chính quy và hàm preg_replace tìm các //OUT: Ví dụ về hàm preg_replace 2000 2000 Ví dụ 2 $replacement đặc biệtVí dụ sau, cho chuỗi có nội dung: //OUT:Năm 2017, Tháng 12 Với ví dụ trên về Ví dụ 3 dùng preg_replace với mảng xử lý BBCODEVí dụ sau sẽ chuyển các mã bbcode: $1', '$1', '$1', '', '$1', '$2' ); $str = preg_replace ($search, $replace, $str); return $str; } //Test $bbcdoe = '[url=https://xuanthulab.net][b]xuanthulab.net[/b][/url]'; echo bbcode2Html($bbcdoe); //OUT HTML: xuanthulab.net Situation I want to use Example from
to
How should I write the replacement string?
This is just an example, so suggesting asked Aug 2, 2013 at 23:54 MightyPorkMightyPork 17.6k10 gold badges73 silver badges128 bronze badges The solution is to wrap the backreference in
which will output a8bcde8fghi8j See the manual on this special case with backreferences. answered Aug 2, 2013 at 23:59 4 You can do this:
Here is a relevant quote from the docs regarding backreference:
answered Aug 3, 2013 at 0:00 jh314jh314 26.2k15 gold badges61 silver badges81 bronze badges Not the answer you're looking for? Browse other questions tagged php regex escaping backreference or ask your own question. |