本文介绍了删除以特定字母/字符开头的单词 php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的例子:

$correction = "game";
$text ="Hello this is an example of a $word that starts with a $dollar sign";
$text = str_replace("\$word",$correct,$text);

如果我回显 $text 它会说:你好,这是一个以美元符号开头的游戏示例.这工作得很好.现在我希望能够将任何以美元符号开头的单词更改为游戏所以它会改变所有以 $ 开头的单词并将它们变成游戏"这个词有什么想法吗?

if I echo $text it will say : hello this is an example of a game that starts with a dollar sign. that works just fine.Now I want to be able to change any word that starts with a dollar sign into gameso it will change all the words that start with $ and turn them into the word "game"any ideas please ?

推荐答案

为此你需要一个正则表达式:

You'll need a regex for this:

$text = preg_replace( '/\$[a-z]+/i', 'game', $text);

这篇关于删除以特定字母/字符开头的单词 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:05