另一个 preg_replace 问题!

发布于 2024-08-07 14:24:06 字数 442 浏览 6 评论 0原文

我有一个字符串,其中的单词聚集在一起,我需要将它们分开,每个以“A”结尾的单词可能应该在一个新行上,

item onea second itema third

我还需要检查以“A”结尾的单词是否实际上应该以“A”结尾“A”就像额外的或苏丹娜。

item oneasecond itemand an extra item

我有一个数组,其中包含来自该网站的以“A”结尾的单词 http://www.morewords .com/ends-with/a 所以我只需要 preg_replace 函数。

每当有人在这里回答问题时,我真的都在学习,再次感谢大家的时间和耐心

Ive got a string that are words bunched together and I need to seperate them, and every word that ends in 'A' should probably be on a new line,

item onea second itema third

I also need to check if the word ending in 'A' should actually end in 'A' like extra or sultana.

item oneasecond itemand an extra item

I have an array full of words ending in 'A' from this website http://www.morewords.com/ends-with/a so I just need the preg_replace function.

I really am learning everytime someone answers a question here so again thanks for everyones time and patience

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

滥情稳全场 2024-08-14 14:24:06

你可以这样做:

// assoc array keyed on words that end with A
$endsWithA = array("sultana" => 1, ...); 

$words = split(' ', $string);

$newString = '';
$finalStrings = array();

foreach ($words AS $w) {    
    // if it ends with a, check to see if it's a real word.
    // if so, end the current string and store it
    if (preg_match("/a$/", $w) && !$endsWithA[$w]) {
        $w = preg_replace("/a$/","", $w);
        $newString .= $w;
        $finalStrings[] = $newString;
        $newString = '';
    }
    else {
        $newString .= $w . ' ';
    }    
}

// Get any remaining newString
if ($newString) $finalStrings[] = trim($newString);

print_r($finalStrings);

还没有测试它等,但它会给你一个数组 $finalStrings ,其中填充了从原始字符串中分割出来的字符串。

更新:修复了代码中的几个拼写错误。

You could do something like this:

// assoc array keyed on words that end with A
$endsWithA = array("sultana" => 1, ...); 

$words = split(' ', $string);

$newString = '';
$finalStrings = array();

foreach ($words AS $w) {    
    // if it ends with a, check to see if it's a real word.
    // if so, end the current string and store it
    if (preg_match("/a$/", $w) && !$endsWithA[$w]) {
        $w = preg_replace("/a$/","", $w);
        $newString .= $w;
        $finalStrings[] = $newString;
        $newString = '';
    }
    else {
        $newString .= $w . ' ';
    }    
}

// Get any remaining newString
if ($newString) $finalStrings[] = trim($newString);

print_r($finalStrings);

Haven't tested it, etc., but it would give you an array $finalStrings populated with the strings split from the original.

Update: fixed a couple of typos in the code.

眼眸 2024-08-14 14:24:06

这听起来更像是 preg_match 的工作。

This sounds more like a job for preg_match.

相思故 2024-08-14 14:24:06

不确定“每个以“A”结尾的单词可能应该换行”是什么意思。如果您在输入字符串之外发布实际输出,总是很有帮助的。

您的意思是一个以“a”结尾的单词后面应该跟一个新行(1)?或者以“a”结尾的单词前面应该有一个新行?或者也许是两者的组合,使以“a”结尾的单词放在自己的行上(在单词之前和之后放置换行符)?

$words = "item onea second itema third";
print_r(preg_split("/\s+(?=\S+a)/i", $words));           // 1
print_r(preg_split("/(?<=a)\s+/i", $words));             // 2
print_r(preg_split("/(?<=a)\s+|\s+(?=\S+a)/i", $words)); // 3

Not sure what you mean by 'and every word that ends in 'A' should probably be on a new line'. Always helpful if you post actual output besides the input-string.

You mean that a word ending with an 'a' should be followed by a new line (1)? Or that a word ending with an 'a' should have a new line before it? Or perhaps a combination of the two, making a word ending with an 'a' be placed on their own line (a line break placed before and after the word)?

$words = "item onea second itema third";
print_r(preg_split("/\s+(?=\S+a)/i", $words));           // 1
print_r(preg_split("/(?<=a)\s+/i", $words));             // 2
print_r(preg_split("/(?<=a)\s+|\s+(?=\S+a)/i", $words)); // 3
迟到的我 2024-08-14 14:24:06

考虑一下,explode() 字符串可能很有用,以便将其分隔成单词数组:

$words = explode(' ', $string);

如果它们由空格分隔。

然后,您可以循环遍历数组 $words 并检查每个数组是否有最后的“a”,必要时对其进行修剪。

preg_replace() 并不总是能满足您的文本操作需求。

编辑:如果您要对 $words 的每个元素使用 preg_replace 那么

foreach ($words as $word) {
    $word = preg_replace('/(\w)a$/', '\1', $word);
}

请注意,我还没有尝试过这个,而且我也没有这样做现在回想一下,如果这实际上改变了数组,但我认为正则表达式应该是正确的。重要的概念是a$,即单字字符串末尾的字母a。我认为这是用字母替换字符串末尾的字母 (\w) 后跟“a”的正确语法但它非常来晚了,我的大脑可能不工作了

另外,我们没有考虑您列出的大约 2900 个以“a”结尾的单词(其中一些我什至从未听说过

Consider that it may be useful to explode() the string in order to separate it into an array of words:

$words = explode(' ', $string);

If they're separated by spaces.

Then you could loop through the array $words and check each one for a final 'a', rtrimming it if necessary.

preg_replace() is not always going to be the answer to your text manipulation needs.

EDIT: If you were to use preg_replace for each element of $words then

foreach ($words as $word) {
    $word = preg_replace('/(\w)a$/', '\1', $word);
}

Note, I haven't tried this, and I don't recall right now if this actually changes the array, but I think the regular expression should be about right. The important concept being a$, that is, a letter a at the end of the one-word string. I think that's the right syntax for replacing a letter (\w) followed by an 'a' at the end of a string with just the letter but it's very late here and my brain may not be working.

Plus, we're not taking into account your list of about 2900 words ending in 'a' (some of which I've never even heard of)

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