当两个字符序列之间出现时,替换/删除特定字符的最佳方法
我有一个PHP函数,该功能从2个不同字符序列之间的字符串中选择文本。
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$fullstring = ',""Word1, Word2""';
$parsed = get_string_between($fullstring, ',""', '""');
echo $parsed; //Result = Word1, Word2
但是,我想进一步扩展此内容,以选择字符串中有多个出现时的所有匹配项(这很可能,因为字符串将由具有数百行和数百个匹配的CSV文件生成。
$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';
)在每个子弦中,我需要删除某些字符。在此示例中,我需要删除逗号。
预期的输出将是:
//Result2 = ',""Word1 Word2"" and another thing ,""Word3 Word4""'
任何人都可以提出实现这一目标的最直接方法吗?谢谢。
I have a php function which selects the text from a string between 2 different character sequences.
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$fullstring = ',""Word1, Word2""';
$parsed = get_string_between($fullstring, ',""', '""');
echo $parsed; //Result = Word1, Word2
However, I would like to extend this further to select all matches when there are multiple occurrences within the string (this is likely, since the string will be generated by a csv file with hundreds of lines and hundreds of matches.) For example:
$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';
And within each substring I will need to remove certain characters. In this example, I need to remove commas.
The intended output would be:
//Result2 = ',""Word1 Word2"" and another thing ,""Word3 Word4""'
Can anybody suggest the most straightforward way of achieving this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,我要求输出这是一件好事,因为最初我还有其他东西。许多人会在这里使用正则表达式,但是我经常发现很难使用,所以我采用了一种更基本的方法:
输出将是:
示例代码
通过删除
$ changethissection
布尔值:示例代码
并通过删除
$ output
数组:示例代码
So it was a good thing I asked for the output, because initially I had something else. Many people would use regular expressions here, but I often find those difficult to work with, so I took a more basic approach:
The output would be:
See: Example code
Slightly more optimized, by removing the
$changeThisSection
boolean:See: Example code
And further optimized, by removing the
$output
array:See: Example code
实际上,您可以以
start
和end
substrings以非贪婪的方式执行匹配所有字符,并使用preg_match_all
捕获如下所示的所有这些中间字符串:在线演示
更新:
如果您希望执行整个单词匹配,您可以简单地进行贪婪的匹配,以
?
使用preg_match
喜欢下面:You can actually perform a regex match matching all characters in between
start
andend
substrings in a non greedy manner and usepreg_match_all
to capture all of those in-between strings like below:Online Demo
Update:
If you wish to perform the whole word match, you can simply do a greedy match removing the
?
withpreg_match
like below:Online Demo
您可以简单地使用字符串替换函数,如果您要删除特定的字符串,我只是将这些功能传递给数组中并用Blanck空间代替。
You can simply use string replace function if you have specific strings to remove, I just passed those in array and replaced it with blanck space.