当两个字符序列之间出现时,替换/删除特定字符的最佳方法

发布于 2025-01-27 19:56:49 字数 812 浏览 1 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笔落惊风雨 2025-02-03 19:56:49

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

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""

示例代码

通过删除$ 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);

示例代码

并通过删除$ 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);

示例代码

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:

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);

The output would be:

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

See: Example code

Slightly more optimized, by removing the $changeThisSection boolean:

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);

See: Example code

And further optimized, by removing the $output array:

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);

See: Example code

孤千羽 2025-02-03 19:56:49

实际上,您可以以startend substrings以非贪婪的方式执行匹配所有字符,并使用 preg_match_all 捕获如下所示的所有这些中间字符串:

<?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]);

在线演示

更新:

如果您希望执行整个单词匹配,您可以简单地进行贪婪的匹配,以使用 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] ?? []);

You can actually perform a regex match matching all characters in between start and end substrings in a non greedy manner and use preg_match_all to capture all of those in-between strings like below:

<?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

Update:

If you wish to perform the whole word match, you can simply do a greedy match removing the ? with preg_match like below:

<?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

不爱素颜 2025-02-03 19:56:49

您可以简单地使用字符串替换函数,如果您要删除特定的字符串,我只是将这些功能传递给数组中并用Blanck空间代替。

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

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.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文