Hướng dẫn code function trang Xóa các ký tự đặc biệt trong một đoạn văn bản
Để xóa các ký tự đặc biệt trong một đoạn văn bản code nhưng vẫn giữ lại định dạng HTML như xuống hàng, in đậm, màu sắc,… bạn có thể sử dụng một shortcode trong WordPress.
Thêm đoạn code sau vào file functions.php
của theme Flatsome (hoặc child theme để tránh mất khi cập nhật):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
// xoa ky tu dac biet function remove_special_chars_page_shortcode() { ob_start(); ?> <form id="remove-special-chars-form"> <label for="chars-to-remove"><strong>Nhập các ký tự cần xóa (mỗi ký tự một dòng):</strong></label><br> <textarea id="chars-to-remove" name="chars-to-remove" rows="5" placeholder="Ví dụ:\n@\n#\n$" style="width: 100%; margin-bottom: 10px;"></textarea><br> <label for="text-to-process"><strong>Nhập văn bản cần xử lý:</strong></label><br> <textarea id="text-to-process" name="text-to-process" rows="6" placeholder="Nhập văn bản vào đây..." style="width: 100%; margin-bottom: 10px;"></textarea><br> <button type="button" id="process-text-btn" style="background-color: #0073aa; color: #fff; padding: 10px 15px; border: none; cursor: pointer;"> Xóa ký tự đặc biệt </button> </form> <div id="processed-result" style="margin-top: 20px; padding: 10px; border: 1px solid #ccc; background-color: #f9f9f9;"> <strong>Kết quả xử lý:</strong> <div id="result-content" style="white-space: pre-wrap; margin-top: 10px;"></div> <button type="button" id="copy-text-btn" style="background-color: #28a745; color: #fff; padding: 10px 15px; border: none; cursor: pointer; margin-top: 10px;"> Sao chép văn bản mới </button> </div> <script> document.getElementById('process-text-btn').addEventListener('click', function() { const charsToRemove = document.getElementById('chars-to-remove').value.trim(); const textToProcess = document.getElementById('text-to-process').value; if (!textToProcess) { alert("Vui lòng nhập văn bản cần xử lý."); return; } if (!charsToRemove) { alert("Vui lòng nhập các ký tự cần xóa."); return; } // Tách các ký tự cần xóa thành mảng (mỗi dòng một ký tự) const charsArray = charsToRemove.split('\n').map(char => char.trim()).filter(char => char); // Tạo regex từ các ký tự cần xóa const regex = new RegExp(`[${charsArray.map(c => c.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('')}]`, 'g'); // Xóa các ký tự trong văn bản const processedText = textToProcess.replace(regex, ''); // Hiển thị kết quả document.getElementById('result-content').innerHTML = processedText; }); // Nút sao chép văn bản document.getElementById('copy-text-btn').addEventListener('click', function() { const resultContent = document.getElementById('result-content').innerText; // Sao chép vào clipboard navigator.clipboard.writeText(resultContent).then(() => { alert("Đã sao chép văn bản mới vào clipboard."); }).catch(err => { alert("Không thể sao chép văn bản."); }); }); </script> <?php return ob_get_clean(); } add_shortcode('remove_special_chars_page', 'remove_special_chars_page_shortcode'); //-------------- |
[=_=remove_special_chars_page]
vào một trang hoặc bài viết trên WordPress
Hướng Dẫn Sử Dụng Giao Diện
- Bước 1: Nhập các ký tự cần xóa vào ô đầu tiên (ví dụ:
@#$%^&*()
). - Bước 2: Nhập đoạn văn bản cần xử lý vào ô thứ hai.
- Bước 3: Nhấn nút “Xóa ký tự đặc biệt”.
- Kết quả: Văn bản đã được xử lý sẽ hiển thị trong phần “Kết quả xử lý”.
Quý khách cần tư vấn thiết kế web giá rẻ, vui lòng nhấn nút bên dưới:
Chào ! Bạn thấy nội dung này thế nào?
Xem thêm: