另一个 preg_replace 问题!
我有一个字符串,其中的单词聚集在一起,我需要将它们分开,每个以“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以这样做:
还没有测试它等,但它会给你一个数组 $finalStrings ,其中填充了从原始字符串中分割出来的字符串。
更新:修复了代码中的几个拼写错误。
You could do something like this:
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.
这听起来更像是 preg_match 的工作。
This sounds more like a job for preg_match.
不确定“每个以“A”结尾的单词可能应该换行”是什么意思。如果您在输入字符串之外发布实际输出,总是很有帮助的。
您的意思是一个以“a”结尾的单词后面应该跟一个新行(1)?或者以“a”结尾的单词前面应该有一个新行?或者也许是两者的组合,使以“a”结尾的单词放在自己的行上(在单词之前和之后放置换行符)?
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)?
考虑一下,
explode()
字符串可能很有用,以便将其分隔成单词数组:如果它们由空格分隔。
然后,您可以循环遍历数组
$words
并检查每个数组是否有最后的“a”,必要时对其进行修剪。preg_replace()
并不总是能满足您的文本操作需求。编辑:如果您要对
$words
的每个元素使用preg_replace
那么请注意,我还没有尝试过这个,而且我也没有这样做现在回想一下,如果这实际上改变了数组,但我认为正则表达式应该是正确的。重要的概念是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: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
thenNote, 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)