Maison >développement back-end >tutoriel php >PHP remplacer
La fonction replace() est une fonction en PHP qui traite principalement des chaînes et vise à remplacer n'importe quelle chaîne en effectuant une recherche de la chaîne qui doit être remplacée. Le modèle de recherche de chaîne peut être tel que soit la chaîne de recherche entière doit être remplacée, soit il peut s'agir d'un tableau qui peut être remplacé par la chaîne de remplacement recherchée dans une chaîne ou un tableau donné. La fonction replace() est une fonction intégrée en PHP qui renvoie une nouvelle chaîne ou un nouveau tableau avec les valeurs remplacées. Cette fonction accepte obligatoirement quatre paramètres ou arguments à savoir search_val, replace_val, subject_val et count.
PUBLICITÉ Cours populaire dans cette catégorie DEVELOPPEUR PHP - Spécialisation | Série de 8 cours | 3 tests simulésCommencez votre cours de développement de logiciels libres
Développement Web, langages de programmation, tests de logiciels et autres
Vous trouverez ci-dessous la syntaxe :
str_replace(search_val, replace_val, subject_val, count)
Le flux syntaxique est d'une manière où les paramètres sont représentés comme suit :
La fonction replace() en PHP est une fonction intégrée qui est utilisée pour remplacer tous les paramètres possibles qui doivent être remplacés dans la chaîne donnée. Elle a donc certains critères de travail qui doivent être suivis :
Given below are the examples mentioned :
This program demonstrates the replace() function in PHP which first finds for the string and then replaces the value of the string with some part as defined. It makes the entire php string replaced with some value as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <p>Let's Find for the string "Life_in_writing" and replace the value of writing with "Anusua"</p> <?php echo str_replace("writing","Anusua","Life_in_writing!"); ?> </body> </html>
Output
This program demonstrates an array of fruit which is trying to replace the particular value of the fruit like guava string with apple as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <p>Find the array with fruit guava and then substitute it with another fruit Apple.</p> <?php $ar_ray = array("guava","kiwi","apple","orange"); print_r(str_replace("guava","apple",$ar_ray,$k)); echo "<br>" . "replaced_fruit: $k"; ?> </body> </html>
Output:
This program demonstrates the substitution of string with elements and values of String with some subject_val string as the string value is less it will get substituted easily as show shown in the output.
Code:
<!DOCTYPE html> <html> <body> <?php $search = array("Welcome","Everyone!"); $replace_str = array("Zee"); $arr_ay = array("Welcome","All",":)"); print_r(str_replace($search,$replace_str,$arr_ay)); ?> </body> </html>
Output:
This program demonstrates the ireplace_string() which acts as case insensitive when compared with the replace() function as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <p>find the string "All is good" and then make it replaced with the capital letter EVERYONE!</p> <?php echo str_ireplace("EVERYONE!","Anusua","All is good"); ?> </body> </html>
Output:
PHP replace() is a function which gives programmers flexibility and scope of re-usability at the time of execution and lets user to use function for searching and replacing string accordingly. It gives users a view of implementation in terms of requirement when it comes to adopting the functionality. Overall a function which plays a significant role in PHP.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!