2017-10-08 102 views
-2

我已经示例文本:如何更换符号函

$text = "I've got a many web APP=. My app= is not working fast. It'= slow app="; 

有必要更换符号“=”以字母“S”由以下条件:如果右或左“=”符号有一个字母,然后用字母“s”代替。教授母语中的字母寄存器。如果你创建三种功能会更好。一个教父母注册,第二个教授注册并代替大写字母“S”,第三个代替小写字母“s”。因此,会有三个结果:

我有很多网络APP S。我的应用s工作不快。这小号缓慢的应用程序小号 - 不区分大小写的正则表达式替换变异

我有很多Web APP 小号。我的应用程序S工作不快。这小号缓慢的应用程序小号 - 大写的正则表达式替换变异

我有很多Web APP 小号。我的应用s工作不快。这小号缓慢的应用程序小号小写的正则表达式替换变异

我长的代码在这里:

$search = array("a=", "b=", "c=", "d=", "e=", "f=", "g=", "h=", "i=","j=", "k=", "l=", "m=", "o=", "p=","r=", .... , "z="); 
$replace s array("as", "bs", "cs", "ds", "es", "fs", "gs", "hs", "is","js", "ks", "ls", "ms", "os", "ps","rs", .... , "zs"); 
$result = str_ireplace($search, $replace, $text); 
+1

那么你尝试过什么到目前为止,你自己?请发布您的代码。我们不在这里为你做你的工作。我们在这里帮助您修复_your_代码。 – arkascha

+0

我只使用str_replace()。 $ search = array(“a =”,“b =”,“c =”,“d =”,....,z =); $ repl = array(“as”,“bs”,“cs”,“ds”,.....,“zs”); str_replace($ search,$ repl,$ text);我仍然不理解正则表达式@arkascha – John

回答

2

你可以试试这个:

=(?=[\w'-])|(?<=[\w'-])= 

和替换本:

“s”或“S”来获得您想要的结果。

但是,这将满足条件2和3为您的输出。

对于条件1你需要一个以上的操作(如果你坚持到正则表达式,唯一的解决办法):

操作1:

搜索到这一点:

=(?=[A-Z])|(?<=[A-Z])= 

以此代替:

"S" 

操作2:

搜索操作1的合成与此:

=(?=[a-z0-9_'-])|(?<=[a-z0-9_'-])= 

并通过此替换:

"s" 

样品来源:(run here

<?php 

$re11='/=(?=[A-Z])|(?<=[A-Z])=/'; 
$re12= '/=(?=[a-z0-9_\'-])|(?<=[a-z0-9_\'-])=/'; 
$re = '/=(?=[\w\'-])|(?<=[\w\'-])=/'; 
$str = 'I\'ve got a many web APP=. My app= is not working fast. It\'= slow app='; 


echo "\n #### condition 1: all contexual upper or lowercase s \n"; 
$subst = 'S'; 
$result = preg_replace($re11,'S', $str); 
$result = preg_replace($re12,'s', $result); 
echo $result; 




echo "\n ##### condition 2: all small case s \n"; 
$subst = 's'; 
$result = preg_replace($re, $subst, $str); 
echo $result; 

echo "\n ##### condition 3: all upper case S \n"; 
$subst = 'S'; 
$result = preg_replace($re, $subst, $str); 
echo $result; 

?> 

样本输出:

#### condition 1: all contexual upper or lowercase s 
I've got a many web APPS. My apps is not working fast. It's slow apps 
##### condition 2: all small case s 
I've got a many web APPs. My apps is not working fast. It's slow apps 
##### condition 3: all upper case S 
I've got a many web APPS. My appS is not working fast. It'S slow appS 

Demo