replace/remove 出现在 2 个字符序列之间的特定字符的最佳方法

Best way to replace/remove a specific character when it appears between 2 character sequences

我有一个 php 函数,它 select 是来自 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

但是,我想将其进一步扩展到 select 当字符串中多次出现时的所有匹配项(这很可能,因为字符串将由包含数百行的 csv 文件生成,并且数百个匹配项。)例如:

$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';

在每个子字符串中,我需要删除某些字符。在这个例子中,我需要删除逗号。

预期输出为:

//Result2 = ',""Word1 Word2"" and another thing ,""Word3 Word4""'

任何人都可以提出实现此目标的最直接方法吗?谢谢

您实际上可以执行正则表达式匹配,以非贪婪的方式匹配 startend 子字符串之间的所有字符,并使用 preg_match_all 捕获所有这些 in-between 字符串如下:

<?php

$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4"",""Word5, Word6""';
$start = ',""';
$end = '""';
preg_match_all('/'. preg_quote($start) . '(.+?)' . preg_quote($end) . '/', $fullstring, $matches);
print_r($matches[1]);

Online Demo

更新:

如果您希望执行整个单词匹配,您可以简单地进行贪婪匹配,删除 ?preg_match,如下所示:

<?php

$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4"",""Word5, Word6""';
$start = ',""';
$end = '""';
preg_match('/'. preg_quote($start) . '(.*)' . preg_quote($end) . '/', $fullstring, $matches);
print_r($matches[0] ?? []);

Online Demo

所以我要求输出是一件好事,因为最初我有其他东西。很多人会在这里使用正则表达式,但我经常发现它们很难使用,所以我采用了更基本的方法:

function extractWantedStuff($input)
{
    $output = [];
    $sections = explode('""', $input);
    $changeThisSection = false;
    foreach ($sections as $section) {
        if ($changeThisSection) {
            $section = str_replace(',', '', $section);
        }
        $output[] = $section;
        $changeThisSection = !$changeThisSection;
    }
    return implode('""', $output);
}

$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';

echo extractWantedStuff($fullstring);

输出将是:

,""Word1 Word2"" and another thing ,""Word3 Word4""

参见:Example code

通过删除 $changeThisSection 布尔值稍微优化:

function extractWantedStuff($input)
{
    $output = [];
    $sections = explode('""', $input);
    foreach ($sections as $key => $section) {
        if ($key % 2 != 0) { // is $key uneven?
            $section = str_replace(',', '', $section);
        }
        $output[] = $section;
    }
    return implode('""', $output);
}

$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';

echo extractWantedStuff($fullstring);

参见:Example code

并通过删除 $output 数组进一步优化:

function extractWantedStuff($string)
{
    $sections = explode('""', $string);
    foreach ($sections as $key => $section) {
        if ($key % 2 != 0) {
            $sections[$key] = str_replace(',', '', $section);
        }
    }
    return implode('""', $sections);
}

$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';

echo extractWantedStuff($fullstring);

参见:Example code

如果您要删除特定的字符串,您可以简单地使用字符串替换功能,我只是将它们传递到数组中并用空白替换它 space。

function removeExtraCharacters($fullstring, $characters = array()){
    foreach($characters as $char){
        $fullstring = str_replace($char,"", $fullstring);
    }
    return $fullstring;
}
$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';
echo removeExtraCharacters($fullstring, array(',"', '"'));
//output: Word1, Word2 and another thing Word3, Word4