Php 用正则表达式捕捉一个单词
我在一个值中存储了一个文本,我只想获取“eIndFormat”之后的文本。 全文如下:Php 用正则表达式捕捉一个单词,php,regex,preg-replace,Php,Regex,Preg Replace,我在一个值中存储了一个文本,我只想获取“eIndFormat”之后的文本。 全文如下: $desc = 'Folder DIN A4 2-breuk altaarvouw 135g druk glanzend 4/4 kleurig (dubbelzijdige druk) 2-breuk altaarvouw eindformaat: 29,7 cm x 41,9 cm gevouwen eindformaat: 20,95 cm x 29,7 cm gegevensformaat: 29,
$desc = 'Folder DIN A4 2-breuk altaarvouw
135g druk glanzend
4/4 kleurig (dubbelzijdige druk)
2-breuk altaarvouw
eindformaat: 29,7 cm x 41,9 cm
gevouwen eindformaat: 20,95 cm x 29,7 cm
gegevensformaat: 29,9 cm x 42,1 cm';
我想要的是“eIndFormat”之后的值。在这种情况下,这将是:29.7cm x 41.9cm。
我用preg_替换尝试了很多图案。但是没有一个适合我。
我试过这个,但没用:
preg_replace('/\/eindformaat\/.*/', '', $desc);
有人能帮我吗?谢谢。您可以将字符串拆分为行,然后在
:
上分解(如果存在):
// Split lines
$lines = explode("\n", $desc);
foreach ($lines as $line) {
// Split the current line on :
$parts = explode(":", $line);
// If the : was present and we now have 2 segments
// Print the second segment if the first was eidnformaat with whitespace trimmed
if (isset($parts[1]) && $parts[0] == "eindformaat") {
echo trim($parts[1]);
}
}
代替正则表达式,您可以将字符串拆分为行,然后在
:
上分解(如果存在):
// Split lines
$lines = explode("\n", $desc);
foreach ($lines as $line) {
// Split the current line on :
$parts = explode(":", $line);
// If the : was present and we now have 2 segments
// Print the second segment if the first was eidnformaat with whitespace trimmed
if (isset($parts[1]) && $parts[0] == "eindformaat") {
echo trim($parts[1]);
}
}
你不想要,但是为了提取。然后省略转义的\/
斜杠,因为这些斜杠在文本中不存在
preg_match('/eindformaat:(.*)/', $desc, $result);
print $result[1];
您需要在parens中使用(.*)
,因此它将成为结果捕获组[1]
。另外,*
足够了,因为如果没有regex/s
标志,它在换行符之外就不会匹配
正则表达式的实际优势在于,您可以将其设置为超级特定的:
preg_match('/eindformaat: ((\d+|,|cm|\s|x)+)/', $desc, $result);
正如所指出的,如果您真的只想将第一个eIndFormat:
,而不是第二个与前面的单词匹配,那么添加一个^
锚点和/m
正则表达式标志,如下所示:
preg_match('/^eindformaat: ....../m', $desc, $result);
你不想要,但是为了提取。然后省略转义的\/
斜杠,因为这些斜杠在文本中不存在
preg_match('/eindformaat:(.*)/', $desc, $result);
print $result[1];
您需要在parens中使用(.*)
,因此它将成为结果捕获组[1]
。另外,*
足够了,因为如果没有regex/s
标志,它在换行符之外就不会匹配
正则表达式的实际优势在于,您可以将其设置为超级特定的:
preg_match('/eindformaat: ((\d+|,|cm|\s|x)+)/', $desc, $result);
正如所指出的,如果您真的只想将第一个eIndFormat:
,而不是第二个与前面的单词匹配,那么添加一个^
锚点和/m
正则表达式标志,如下所示:
preg_match('/^eindformaat: ....../m', $desc, $result);
这应该适合您:
preg_match('/eindformaat:([a-zA-Z0-9. ,]+)/g', $desc, $result);
您的匹配项将存储在$result的数组中。这将为您匹配它:
preg_match('/eindformaat:([a-zA-Z0-9. ,]+)/g', $desc, $result);
您的匹配项将存储在$result的数组中。请尝试:
$desc = preg_replace('/(\neindformaat:)[^\n]*/i', '$1', $desc);
这将删除“eIndFormat:”之后的值(这似乎是您想要的)。否则,如果要检索该值,则必须使用preg_match 编辑:
下面是一个稍微亮一点的版本:
$desc = preg_replace('/^(eindformaat:).*/im', '$1', $desc);
尝试:
这将删除“eIndFormat:”之后的值(这似乎是您想要的)。否则,如果要检索该值,则必须使用preg_match 编辑:
下面是一个稍微亮一点的版本:
$desc = preg_replace('/^(eindformaat:).*/im', '$1', $desc);
你为什么不知道冒号的位置,然后把它的每一个部分都取对,然后在trim()中运行,这样就失去了前导空间
$line = 'gevouwen eindformaat: 20,95 cm x 29,7 cm';
$the_bit_that_want = trim( substr($line, substr($line, ':') ) );
你为什么不知道冒号的位置,然后把它的每一个部分都取对,然后在trim()中运行,这样就失去了前导空间
$line = 'gevouwen eindformaat: 20,95 cm x 29,7 cm';
$the_bit_that_want = trim( substr($line, substr($line, ':') ) );
不确定如何使用正则表达式,但请尝试使用strpos和substr生成所需的结果
$start_eindformaat = stripos($desc, 'eindformaat') + 13;
$length_eindformaat = stripos($start_eindformaat , 'gevouwen eindformaat') - $start_eindformaat;
$eindformaat = substr($desc, $start_eindformaat, $length_eindformaat
不确定如何使用正则表达式,但请尝试使用strpos和substr生成所需的结果
$start_eindformaat = stripos($desc, 'eindformaat') + 13;
$length_eindformaat = stripos($start_eindformaat , 'gevouwen eindformaat') - $start_eindformaat;
$eindformaat = substr($desc, $start_eindformaat, $length_eindformaat
还将赶上“gevouwen Eindformat”。很好。我不知道这两个词是什么意思,不管怎样,正则表达式只会与第一个匹配。但是一个
^
主播可能是明智的。也会捕捉到“gevouwen Eindformat”。很好。我不知道这两个词是什么意思,不管怎样,正则表达式只会与第一个匹配。但是一个^
主播可能是明智的。它也会捕捉到“gevouwen Eindformat”。它也会捕捉到“gevouwen Eindformat”。我认为你的标题是非常误导人的。也许你想更新一下?看起来你只是想得到一个副标题。我认为你的标题很容易让人误解。也许你想更新一下?看起来你只是想得到一个子串