简体   繁体   English

使用正则表达式用php删除带引号(“)的字符串。

[英]Using regex replace a string with the same string with quotes (") removed using php

I have a multi-line string shown below - 我有一个如下所示的多行字符串-

   ?a="text1
    ?bc="text23

I need to identify a pattern like using below regex 我需要确定一种使用正则表达式的模式

  '/[?][a-z^A-Z]+[=]["]/'

and replace my string by just remove the double quote (") in it, expected output is shown below 并通过删除其中的双引号(“)来替换我的字符串,预期输出如下所示

?a=text1
?b=text23

Please help in solving the above issue using php. 请使用php解决上述问题。

Capture everything except the quote in a capture group () and replace: 除了捕获在捕获组报价的一切()和替换:

$string = preg_replace('/([?][a-z^A-Z]+[=])["]/', '$1', $string);

But you really don't need all those character classes [] : 但是您真的不需要所有这些字符类[]

/(\?[a-z^A-Z]+=)"/

I will give another solution because i see the php tag also. 我将给出另一个解决方案,因为我也看到了php标签。 So let's say you have these: 因此,假设您有以下这些:

$a='"text1';
$b='"text2'; 

if i echo them i get 如果我回应他们,我会得到

"text1
"text2

in order to get rid of double quote there is a function trim in php that you can use like that: 为了摆脱双引号,您可以像这样在php中使用函数修剪:

echo trim($a,'"');
echo trim($b,'"');

the results will be 结果将是

text1
text2

I dont think you need regex in this occasion as long as you use php. 我认为只要您使用php,就不需要在这种情况下使用正则表达式。 Php can take care of those small things without bother with complex regex expressions. Php可以照顾那些小事情,而不必理会复杂的正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM