PHP程序替换字符串中的单词

What if a user has typed his name wrong and now he wants to replace his name with the correct name? This a practical scenario, and PHP even has an inbuilt function to do so. There are the following methods to rectify an error made in the string.

如果用户输入了错误的姓名,现在又想用正确的姓名替换姓名,该怎么办? 这是一个实际的场景,PHP甚至具有内置功能。 有以下方法可纠正字符串中的错误。

1)使用str_replace()函数 (1) Using str_replace() function)

In this method, str_replace() function replaces all the occurrences of the replaceString by the mentioned string.

在此方法中, str_replace()函数将所有出现的replaceString替换为上述字符串。

The function str_replace() is used for searching a regular expression and replacing a specific content in place of all the occurrences of searched expression. It always returns either a string or an array, according to what was searched and replaced. It has 3 compulsory parameters while one is optional. The compulsory parameters of the function are - value to be searched, the value it will be replaced with and the string in which this value needs to be searched. The optional parameter specifies the count of the number of times the replacement is needed.

函数str_replace()用于搜索正则表达式并替换特定内容来代替所有出现的搜索表达式。 它始终根据搜索和替换的内容返回字符串数组 。 它具有3个必选参数,而1个是可选参数。 该功能的强制性参数是-要搜索的值,将替换为的值以及需要在其中搜索该值的字符串。 可选参数指定需要更换的次数。

Syntax:

句法:

str_replace (
    $word_to_replace, 
    $new_word, 
    $string_in_which_word_is_present
    );

Program:

程序:

<?php
    $str= 'Sayesaha';
    
    // message to be printed
    $newstr = 'welcome '. $str . ' to learn about PHP language. We are delighted to have you '.$str;
    
    // printing message 
    echo ($newstr."\n");
    
    // changing the name
    $str1= 'Sayesha';
    echo str_replace($str, $str1 , $newstr);
?>

Output:

输出:

welcome Sayesaha to learn about PHP language. We are delighted to have you Sayesaha
welcome Sayesha to learn about PHP language. We are delighted to have you Sayesha

2)使用str_ireplace()函数 (2) Using str_ireplace() function)

This function does the same thing as str_replace(), i.e. the function str_ireplace() is used for searching a regular expression and replacing a specific content in place of all the occurrences of searched expression, the difference being that str_ireplace() is case insensitive. It is also an inbuilt function of PHP.

此函数与str_replace()的功能相同,即,函数str_ireplace()用于搜索正则表达式并替换特定内容来代替所有出现的搜索表达式,区别在于str_ireplace()不区分大小写。 它也是PHP的内置函数。

Program:

程序:

<?php
    $str = 'sayesha';
    
    // message to be printed
    $newstr = 'welcome ' . $str . ' to learn about PHP language. We are delighted to have you ' . $str;
    
    // printing message
    echo ($newstr . "\n");
    
    // changing the name
    $str1 = 'Sayesha';
    echo str_ireplace($str, $str1, $newstr);
?>

Output:

输出:

welcome sayesha to learn about PHP language. We are delighted to have you sayesha
welcome Sayesha to learn about PHP language. We are delighted to have you Sayesha

3)使用搜索和替换( preg_replace()函数) (3) Using Search and Replace (preg_replace() function))

What if you need to search a word and then replace it. For example, there was an error while typing an article, and the word 'school' needs to be replaced with the word 'college'.

如果您需要搜索一个单词然后替换它,该怎么办。 例如,键入文章时发生错误,需要将“学校”一词替换为“学院”一词。

Syntax:

句法:

preg_replace(
    $word_to_replace, 
    $new_word, 
    $string_to_be_searched, 
    $limit, 
    $count
    );

preg_replace() function is used in PHP for searching a regular expression and replacing a specific content in place of the searched expression. It always returns either a string or an array, according to what was searched and replaced.

在PHP中, preg_replace()函数用于搜索正则表达式并替换搜索到的表达式的特定内容。 它始终根据搜索和替换的内容返回字符串或数组。

Program:

程序:

<?php
    // string
    $str = 'We welcome all of you to our school of PHP.
                This school is one of its kind.
                Many schools dont offer this subject.';
    $s = 'school';
    $newstr = 'college';
    
    // printing message
    echo ('Old message-->' . $str);
    echo ("\n");
    echo ('New message-->');
    
    // Using preg_replace() function
    // to replace the word
    $str = preg_replace('/school/', $newstr, $str);
    
    // Printing the result
    echo $str;
?>

Output:

输出:

Old message-->We welcome all of you to our school of PHP.
                This school is one of its kind.
                Many schools dont offer this subject.
New message-->We welcome all of you to our college of PHP.
                This college is one of its kind.
                Many colleges dont offer this subject.


翻译自: https://www.includehelp.com/php/program-to-replace-a-word-in-a-string.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值