Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php str_replace:仅匹配整个单词_Php_Regex_String_Replace - Fatal编程技术网

Php str_replace:仅匹配整个单词

Php str_replace:仅匹配整个单词,php,regex,string,replace,Php,Regex,String,Replace,由于str_replace()匹配“:Name”中的两次“:Name:Name_en”,因此我只想匹配整个单词的结果。我想切换到preg\u replace(),因为 但这不起作用,因为冒号。没有更换。RegExp会是什么样子 \b是单词边界。但是我认为冒号不属于这样的单词边界。字符串开头不需要单词边界: $str = preg_replace('/:Name\b/i', '"Test"', $str); 如果您使用的是PHP5+,您仍然可以使用str_replace $str = ":Nam

由于
str_replace()
匹配“:Name”中的两次“:Name:Name_en”,因此我只想匹配整个单词的结果。我想切换到
preg\u replace()
,因为

但这不起作用,因为冒号。没有更换。RegExp会是什么样子


\b
是单词边界。但是我认为冒号不属于这样的单词边界。

字符串开头不需要单词边界:

$str = preg_replace('/:Name\b/i', '"Test"', $str);

如果您使用的是PHP5+,您仍然可以使用str_replace

$str = ":Name :Name_en";
echo $str . chr(10);

// The final int limits the function to a single replace.
$str = str_replace(':Name', '"Test"', $str, 1);

echo $str;

如果要替换多个关联关键字(如字典或占位符),可以使用此选项匹配正则表达式模式:

$words=array("_saudation_"=>"Hello", "_animal_"=>"cat", "_animal_sound_"=>"MEooow");
$source=" _saudation_! My Animal is a _animal_ and it says _animal_sound_ ,  _no_match_";

echo (preg_replace_callback("/\b_(\w*)_\b/", function($match) use ($words) { if(isset($words[$match[0]])){
 return ($words[$match[0]]);}else{ return($match[0]);}},  $source));
返回:你好!我的动物是一只猫,它说喵喵,不相配_


注意,尽管“\u no\u match\u”缺少翻译,但它将在正则表达式期间匹配,但保留其键。您可以使用空格来替换其公共字符,以查找句子单词之间的空格

$str = ":Name :Name_en";
echo $str;

// The final int limits the function to a single replace.
$str = str_replace(':Name ', 'Test', $str);

echo $str;

函数removeCommonWords($input){
//停止语
$commonWords=数组(‘a’、‘能够’、‘大约’、‘以上’、‘国外’、‘根据’、‘相应’、‘跨越’、‘实际’、‘调整’、‘之后’、‘再次’、‘反对’、‘以前’、‘前进’、‘不是’、‘全部’、‘允许’、‘几乎’、‘单独’、‘一起’、‘已经’、‘虽然’、‘总是’、‘am’、‘中间’、‘中间’、‘中间’、‘中间’、‘安’、‘另一’、‘任何’)“任何人”、“无论如何”、“任何人”、“任何事”、“无论如何”、“无论如何”、“任何地方”、“分开”、“出现”、“欣赏”、“适当”、“是”、“不是”、“周围”、“作为”、“a的”、“旁白”、“询问”、“相关”、“在”、“可用”、“离开”、“非常”、“b”、“后退”、“后退”、“后退”、“成为”、“因为”、“成为”、“成为”、“成为”、“成为”、“被”“,”之前“,”之前“,”开始“,”后面“,”存在“,”相信“,”下面“,”旁边“,”除了“,”最好“,”更好“,”之间“,”超越“,”两者“,”简短“,”但是“,”由“,”c“,”来“,”来“,”来“,”不能“,”说明“,”原因“,”原因“,”确定“,”改变“,”清楚地“,”c“,”来“,”来“,”来“,”,“因此”、“考虑”、“考虑”、“包含”、“包含”、“包含”、“对应”、“可以”、“不能”、“课程”、“c”、“当前”、“d”、“敢”、“不敢”、“肯定”、“描述”、“尽管”、“做过”、“没有”、“不同”、“直接”、“做过”、“做过”、“不做”、“向下”、“在进行中”、“做过”、“不做”、“向下”、“在进行中”、“ea”“ch”、“edu”、“eg”、“八”、“八十”、“要么”、“其他”、“其他”、“结束”、“足够”、“完全”、“特别”、“et”、“等”、“甚至”、“曾经”、“永远”、“每一个”、“每个人”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每一个”、“每,“following”、“following”、“force”、“forever”、“former”、“forth”、“forward”、“find”、“four”、“from”、“furth”、“进一步”、“g”、“get”、“get”、“get”、“get”、“get”、“go”、“go”、“go”、“good”、“good”、“good”、“get”、“get”、“good”、“greats”、“h”、“had”、“haven”、“haven”、“haven”、“haven”、“haven”、“haven”、“haven”、“haven”、“he”“他会”、“他会”、“你好”、“帮助”、“因此”、“她”、“这里”、“此后”、“在此”、“在此”、“这里”、“这里”、“在此”、“她”、“她自己”、“他”、“你好”、“他”、“他”、“他”、“这里”、“希望”、“如何”、“如何”、“如何”、“然而”、“一百”、“我”、“我会”、“ie”、“如果”、“忽略”、“我会”、“我”、“立即”、“在”、“在”、“内”、“公司”、“公司”、“确实”,“表示”、“表示”、“表示”、“内部”、“内部”、“到目前为止”、“取而代之”、“进入”、“内部”、“是”、“不是”、“它”、“它”、“它将”、“它”、“它自己”、“我已经”、“j”、“只是”、“k”、“保持”、“保持”、“保持”、“知道”、“知道”、“知道”、“知道”、“知道”、“l”、“最近”、“最近”、“最近”、“最近”、“最近”、“最少”、“较少”、“最怕”、“让”、“让”、“像”、“李”“可能”、“类似”、“小”、“看起来”、“看起来”、“看起来”、“低”、“低”、“有限”、“m”、“制造”、“主要”、“制造”、“制造”、“很多”、“可能”、“可能”、“我”、“意思”、“同时”、“仅仅”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“可能”、“不”、“我的”、“可能”、“可能”、“可能”、“可能”、“可能”、“我的”、“我的”、“可能”、“可能”、“更多”、“而且”、“大多数”、“大部分”、“大部分”、“先生”、“夫人”、“必须”、“我的”、“我的”、“我的”、“我的”、“我的”elf、n、name、即、nd、near、near、near、neverf、neverless、neverf、neverless、neverless、new、next、next、next、next、next、none、none、none、noone、noone、none、none、NONO-one、NOTED、NOVERTIVE、NOWER、NOWHORE、o、OBVIOUSSy、of、of、of、ok、ok、ok、old、on、once、one、one、one、one、only、on、Antival、or、others、others、others、others、others、others、others、others、others、others、others、others、others、others、others、others、others、others、others、others、others、others、otherse、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、,“似乎”、“似乎”、“见过”、“自我”、“自我”、“理智”、“被发送”、“严肃”、“严肃”、“七”、“几”、“应该”、“她”、“她”、“应该”、“不应该”、“因为”、“六”、“所以”、“一些”、“某人”、“某天”、“某地”、“某物”、“某时”、“有时”、“某地”、“某地”、“不久”、“对不起”、“等等”指定的、指定的、指定的、静止的、sub的、这样的、sup的、肯定的、t的、take的、take的、take的、take的、take的、take的、take的、take的、than的、than的、than的、than的、than的、thall的、that的、that的、that的、that的、that的、that的、that的、that的、that的、that的、that的、that
$str = ":Name :Name_en";
echo $str;

// The final int limits the function to a single replace.
$str = str_replace(':Name ', 'Test', $str);

echo $str;
function removeCommonWords($input) {

    // EEEEEEK Stop words
    $commonWords = array('a', 'able', 'about', 'above', 'abroad', 'according', 'accordingly', 'across', 'actually', 'adj', 'after', 'afterwards', 'again', 'against', 'ago', 'ahead', 'ain\'t', 'all', 'allow', 'allows', 'almost', 'alone', 'along', 'alongside', 'already', 'also', 'although', 'always', 'am', 'amid', 'amidst', 'among', 'amongst', 'an', 'and', 'another', 'any', 'anybody', 'anyhow', 'anyone', 'anything', 'anyway', 'anyways', 'anywhere', 'apart', 'appear', 'appreciate', 'appropriate', 'are', 'aren\'t', 'around', 'as', 'a\'s', 'aside', 'ask', 'asking', 'associated', 'at', 'available', 'away', 'awfully', 'b', 'back', 'backward', 'backwards', 'be', 'became', 'because', 'become', 'becomes', 'becoming', 'been', 'before', 'beforehand', 'begin', 'behind', 'being', 'believe', 'below', 'beside', 'besides', 'best', 'better', 'between', 'beyond', 'both', 'brief', 'but', 'by', 'c', 'came', 'can', 'cannot', 'cant', 'can\'t', 'caption', 'cause', 'causes', 'certain', 'certainly', 'changes', 'clearly', 'c\'mon', 'co', 'co.', 'com', 'come', 'comes', 'concerning', 'consequently', 'consider', 'considering', 'contain', 'containing', 'contains', 'corresponding', 'could', 'couldn\'t', 'course', 'c\'s', 'currently', 'd', 'dare', 'daren\'t', 'definitely', 'described', 'despite', 'did', 'didn\'t', 'different', 'directly', 'do', 'does', 'doesn\'t', 'doing', 'done', 'don\'t', 'down', 'downwards', 'during', 'e', 'each', 'edu', 'eg', 'eight', 'eighty', 'either', 'else', 'elsewhere', 'end', 'ending', 'enough', 'entirely', 'especially', 'et', 'etc', 'even', 'ever', 'evermore', 'every', 'everybody', 'everyone', 'everything', 'everywhere', 'ex', 'exactly', 'example', 'except', 'f', 'fairly', 'far', 'farther', 'few', 'fewer', 'fifth', 'first', 'five', 'followed', 'following', 'follows', 'for', 'forever', 'former', 'formerly', 'forth', 'forward', 'found', 'four', 'from', 'further', 'furthermore', 'g', 'get', 'gets', 'getting', 'given', 'gives', 'go', 'goes', 'going', 'gone', 'got', 'gotten', 'greetings', 'h', 'had', 'hadn\'t', 'half', 'happens', 'hardly', 'has', 'hasn\'t', 'have', 'haven\'t', 'having', 'he', 'he\'d', 'he\'ll', 'hello', 'help', 'hence', 'her', 'here', 'hereafter', 'hereby', 'herein', 'here\'s', 'hereupon', 'hers', 'herself', 'he\'s', 'hi', 'him', 'himself', 'his', 'hither', 'hopefully', 'how', 'howbeit', 'however', 'hundred', 'i', 'i\'d', 'ie', 'if', 'ignored', 'i\'ll', 'i\'m', 'immediate', 'in', 'inasmuch', 'inc', 'inc.', 'indeed', 'indicate', 'indicated', 'indicates', 'inner', 'inside', 'insofar', 'instead', 'into', 'inward', 'is', 'isn\'t', 'it', 'it\'d', 'it\'ll', 'its', 'it\'s', 'itself', 'i\'ve', 'j', 'just', 'k', 'keep', 'keeps', 'kept', 'know', 'known', 'knows', 'l', 'last', 'lately', 'later', 'latter', 'latterly', 'least', 'less', 'lest', 'let', 'let\'s', 'like', 'liked', 'likely', 'likewise', 'little', 'look', 'looking', 'looks', 'low', 'lower', 'ltd', 'm', 'made', 'mainly', 'make', 'makes', 'many', 'may', 'maybe', 'mayn\'t', 'me', 'mean', 'meantime', 'meanwhile', 'merely', 'might', 'mightn\'t', 'mine', 'minus', 'miss', 'more', 'moreover', 'most', 'mostly', 'mr', 'mrs', 'much', 'must', 'mustn\'t', 'my', 'myself', 'n', 'name', 'namely', 'nd', 'near', 'nearly', 'necessary', 'need', 'needn\'t', 'needs', 'neither', 'never', 'neverf', 'neverless', 'nevertheless', 'new', 'next', 'nine', 'ninety', 'no', 'nobody', 'non', 'none', 'nonetheless', 'noone', 'no-one', 'nor', 'normally', 'not', 'nothing', 'notwithstanding', 'novel', 'now', 'nowhere', 'o', 'obviously', 'of', 'off', 'often', 'oh', 'ok', 'okay', 'old', 'on', 'once', 'one', 'ones', 'one\'s', 'only', 'onto', 'opposite', 'or', 'other', 'others', 'otherwise', 'ought', 'oughtn\'t', 'our', 'ours', 'ourselves', 'out', 'outside', 'over', 'overall', 'own', 'p', 'particular', 'particularly', 'past', 'per', 'perhaps', 'placed', 'please', 'plus', 'possible', 'presumably', 'probably', 'provided', 'provides', 'q', 'que', 'quite', 'qv', 'r', 'rather', 'rd', 're', 'really', 'reasonably', 'recent', 'recently', 'regarding', 'regardless', 'regards', 'relatively', 'respectively', 'right', 'round', 's', 'said', 'same', 'saw', 'say', 'saying', 'says', 'second', 'secondly', 'see', 'seeing', 'seem', 'seemed', 'seeming', 'seems', 'seen', 'self', 'selves', 'sensible', 'sent', 'serious', 'seriously', 'seven', 'several', 'shall', 'shan\'t', 'she', 'she\'d', 'she\'ll', 'she\'s', 'should', 'shouldn\'t', 'since', 'six', 'so', 'some', 'somebody', 'someday', 'somehow', 'someone', 'something', 'sometime', 'sometimes', 'somewhat', 'somewhere', 'soon', 'sorry', 'specified', 'specify', 'specifying', 'still', 'sub', 'such', 'sup', 'sure', 't', 'take', 'taken', 'taking', 'tell', 'tends', 'th', 'than', 'thank', 'thanks', 'thanx', 'that', 'that\'ll', 'thats', 'that\'s', 'that\'ve', 'the', 'their', 'theirs', 'them', 'themselves', 'then', 'thence', 'there', 'thereafter', 'thereby', 'there\'d', 'therefore', 'therein', 'there\'ll', 'there\'re', 'theres', 'there\'s', 'thereupon', 'there\'ve', 'these', 'they', 'they\'d', 'they\'ll', 'they\'re', 'they\'ve', 'thing', 'things', 'think', 'third', 'thirty', 'this', 'thorough', 'thoroughly', 'those', 'though', 'three', 'through', 'throughout', 'thru', 'thus', 'till', 'to', 'together', 'too', 'took', 'toward', 'towards', 'tried', 'tries', 'truly', 'try', 'trying', 't\'s', 'twice', 'two', 'u', 'un', 'under', 'underneath', 'undoing', 'unfortunately', 'unless', 'unlike', 'unlikely', 'until', 'unto', 'up', 'upon', 'upwards', 'us', 'use', 'used', 'useful', 'uses', 'using', 'usually', 'v', 'value', 'various', 'versus', 'very', 'via', 'viz', 'vs', 'w', 'want', 'wants', 'was', 'wasn\'t', 'way', 'we', 'we\'d', 'welcome', 'well', 'we\'ll', 'went', 'were', 'we\'re', 'weren\'t', 'we\'ve', 'what', 'whatever', 'what\'ll', 'what\'s', 'what\'ve', 'when', 'whence', 'whenever', 'where', 'whereafter', 'whereas', 'whereby', 'wherein', 'where\'s', 'whereupon', 'wherever', 'whether', 'which', 'whichever', 'while', 'whilst', 'whither', 'who', 'who\'d', 'whoever', 'whole', 'who\'ll', 'whom', 'whomever', 'who\'s', 'whose', 'why', 'will', 'willing', 'wish', 'with', 'within', 'without', 'wonder', 'won\'t', 'would', 'wouldn\'t', 'x', 'y', 'yes', 'yet', 'you', 'you\'d', 'you\'ll', 'your', 'you\'re', 'yours', 'yourself', 'yourselves', 'you\'ve', 'z', 'zero');

    return preg_replace('/\b(' . implode('|', $commonWords) . ')\b/i', '', $input);
}

//Test

$s = "It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.";

echo $s . "<br>";
echo removeCommonWords($s);