如何将符号替换为字母

How to replace symbol to letter

我有示例文本:

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

以下情况需要用字母"s"替换符号“=”:如果“=”符号右边或左边有字母则用字母[=替换48=]。教授母词中的字母记号。如果创建三种功能会更好。一个教父寄存器,第二个不教寄存器,用大写字母"S"代替,第三个用小写字母"s"代替。结果会出现三个结果:

我有很多网页APPS。我的应用程序s 运行不快。 It's 慢应用程序s - 不区分大小写的正则表达式替换 variant

我有很多网页APPS。我的应用程序S 运行不快。 It'S slow appS - 大写正则表达式替换 variant

我有很多网页APPs。我的应用程序s 运行不快。它's 慢应用程序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);

你可以试试这个:

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

并替换为:

"s" 或 "S" 以获得您想要的结果。

然而,这将满足您的输出条件 2 和 3。

对于条件 1,您需要不止一项操作(如果您坚持使用仅正则表达式的解决方案):

操作一:

按此搜索:

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

替换为:

"S"

操作二:

用这个搜索操作 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