I have a problem. I want to replace certain strings only if they are exactly like I typed. So if there is a string with 5 Eurhe should only be replaced with e.g. Steam 5 Euro, if he stands alone and not if the string is like How are you 5 Eur pls.With my actual code this is not possible... I use e.g.: $string = str_replace('Apple Itunes 25 Euro Guthaben Prepaid De', 'Apple iTunes 25 Euro', $string)Because here the string contains 25 Eur this code is also adding some stuff:$string = str_replace('25 Eur', 'Steam 25 Euro', $string);But if I want to use preg_replace(/\b25 Eur\b/i) I get this error:So I have two questions:How can I use an multibyte replace function?How can I tell this function only to replace a certain string if he stands alone and not if he contains the searched string?Greetings and Thank You! 解决方案 This Should Work.Search by:^(25 Eur)$Replace with:Steam 25 EuroInput:Apple Itunes 25 Euro Guthaben Prepaid De', 'Apple iTunes 25 Euro25 EurOutput:Apple Itunes 25 Euro Guthaben Prepaid De', 'Apple iTunes 25 EuroSteam 25 EuroPHP Code:<?php$re = '/^(25 Eur)$/m';$str = 'Apple Itunes 25 Euro Guthaben Prepaid De\', \'Apple iTunes 25 Euro25 Eur';$subst = 'Steam 25 Euro';$result = preg_replace($re, $subst, $str);echo "The result of the substitution is ".$result;See: https://regex101.com/r/3DKEas/2 这篇关于仅当字符串为搜索时才替换字符串(preg_replace多字节)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 01:08