[英]How do I extract a word between pipes using regex in PHP?
I'm writing a parser and I need to extract words that are between double pipes using php 我正在编写一个解析器,我需要使用php提取双管道之间的单词
For example I want to extract the 'ipsum' from the string below 例如,我想从下面的字符串中提取“ ipsum”
Lorem ||ipsum|| blah
If there are multiple words between double pipes, they should be extracted as well 如果双管道之间有多个单词,则也应提取它们
Clarification 澄清度
When I say multiple words I don't mean this: ||word another word|| 当我说多个单词时,我不是这个意思:||另一个单词||
I mean this 我的意思是
||Word1|| blah blah (newline)
blah ||Word2||
Clarification part 2 澄清第2部分
the ||quick|| brown fox ||jumps|| over the lazy ||dog||
What should be extracted should be the words 'quick', 'jumps' and 'dog' 应该提取的单词应该是“快速”,“跳跃”和“狗”
Sorry for the confusion... There probably are some right answers below, I'll pick one once I confirm it tomorrow at work :) 抱歉给您带来的困惑...下面可能有一些正确的答案,一旦我明天工作确认,我就选一个。
What about a simple 那么简单
$array = explode('||', $string);
After that, you probably want to trim the array values using trim(). 之后,您可能想使用trim()修剪数组值。
See also http://www.php.net/explode and http://www.php.net/trim 另请参见http://www.php.net/explode和http://www.php.net/trim
Here is a regex solution: http://regex101.com/r/vE9pY9 这是一个正则表达式解决方案: http : //regex101.com/r/vE9pY9
/\Q||\E[^|]+\Q||\E/
This will not accept pipes to be a part of the word though. 但是,这不会接受管道成为该词的一部分。 If that is a requirement the regex has to be remade.
如果这是必需的,则必须重新制作正则表达式。
Try this: 尝试这个:
if(preg_match('/\|\|(.*)\|\|/', $str, $matches) === 1){
echo $matches[1];
}
Or if there are multiple ||
或者如果有多个
||
, try this: , 尝试这个:
if(preg_match_all('/\|\|(.*?)\|\|/', $str, $matches) !== FALSE){
print_r($matches[1]);
}
I think I know what your looking for: 我想我知道你在找什么:
\|\|[a-zA-Z0-9]+\|\|
This should satisfy your example: 这应该满足您的示例:
||Word1|| blah blah (newline)
blah ||Word2||
Of picking Word1 and Word2 out. 挑选Word1和Word2。
You will need to strip off the ||
您将需要去除
||
on either side. 在任一侧。
There is a way to use regex to strip the ||
有一种使用正则表达式来去除
||
out as well but KISS. 也一样,但吻。 It is easier to read and easier to, in general, strip this stuff out later.
通常更容易阅读,以后再将其删除。 So you have a simple regex with a simple trim.
因此,您有一个带有简单修饰的简单正则表达式。
Hope it helps, 希望能帮助到你,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.