Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

โปรแกรม PHP เพื่อแทนที่คำด้วยสัญลักษณ์อื่นในประโยค


ในการแทนที่คำด้วยสัญลักษณ์อื่นในประโยค โค้ด PHP มีดังต่อไปนี้ -

ตัวอย่าง

<?php
   $my_str = "This is a sample only";
   $search_str = array("sample", "This");
   $replace_str = array("simple");
   $result = str_replace($search_str, $replace_str, $my_str);
   print_r("The final replaced string is :");
   print_r($result);
?>

ผลลัพธ์

The final replaced string is : is a simple only

ในที่นี้ มีการกำหนดสตริง และสตริงที่ต้องการแทนที่ด้วยสตริงอื่นจะอยู่ภายในอาร์เรย์และจัดเรียงเป็นตัวแปร -

$my_str = "This is a sample only";
$search_str = array("sample", "This");
$replace_str = array("simple");

ฟังก์ชัน 'str_replace' ใช้เพื่อแทนที่ค่าด้วยค่าที่ระบุอื่น ผลลัพธ์ถูกพิมพ์บนหน้าจอ −

$result = str_replace($search_str, $replace_str, $my_str);
print_r("The final replaced string is :");
print_r($result);