我有一个以下格式的字符串。
Banking Sector AND Lorem Ispum Dummy text AND Partner OR Agri Business NOT Corporate Finance
我需要使用PHP将此查询转换为以下格式。
"Banking Sector" AND "Lorem Ispum Dummy text" AND "Partner" OR "Agri Business" NOT "Corporate Finance"
我需要在AND / OR / NOT运算符之间为单词添加双引号。我怎么能这样做?
答案 0 :(得分:2)
如果你的字符串没有开头而且没有以(AND / OR / NOT)结束你可以使用这个:
$output = str_replace(array(" AND "," OR "," NOT "),array('" AND "','" OR "','" NOT "'),$input);
$output ='"'.trim($output).'"';
答案 1 :(得分:0)
使用backreferences
和preg_replace("(.+)\s(AND|OR|NOT)\s(.+)", "\"$1\"\s$2\s\"$3\"")
。像这样:
limit = input('Maximum number to factorise: ')
for i in range(1,int(limit)):
line = '{:2}:'.format(i)
for j in range (1,11):
if i % j == 0:
line += "{:4}".format('0')
else:
line += "{:4}".format('-')
print(line)
免责声明:未经测试