PHP使用正则表达式从字符串中提取内容

发布于 2024-11-07 22:10:50 字数 226 浏览 0 评论 0原文

我有以下字符串,我想从中提取“383-0408”,显然内容发生了变化,但零件号始终遵循字符串“Our Stk #:”,如何才能最优雅地从字符串中提取此信息?

Microchip Technology Inc.
18 PIN, 7 KB FLASH, 256 RAM, 16 I/O 

Mfr's Part #: PIC16F648A-I/SO 
Our Stk #: 383-0408

I have the following String and I want to extract the "383-0408" from it, obviously the content changes but the part number always follows the String "Our Stk #:", how can I most elegantly extract this information from the string?

Microchip Technology Inc.
18 PIN, 7 KB FLASH, 256 RAM, 16 I/O 

Mfr's Part #: PIC16F648A-I/SO 
Our Stk #: 383-0408

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

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

发布评论

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

评论(4

辞旧 2024-11-14 22:10:50

您可以这样做:

<?php

$str = "Microchip Technology Inc.
18 PIN, 7 KB FLASH, 256 RAM, 16 I/O 

Mfr's Part #: PIC16F648A-I/SO 
Our Stk #: 383-0408";

if (preg_match('/Our Stk #: (\d*-\d*)/', $str, $matches)) {
    echo $matches[1];
}

仅当您要查找的数字部分始终采用 digits-digits 形式时,此方法才有效。 @Richard86 给出了一个更通用的解决方案,可以使用任意数量和任意数量的破折号,作为您问题的另一个答案。

编辑:

为了避免破折号周围没有数字的情况,正如@Richard86在评论中所说,正则表达式应该如下所示:

if (preg_match('/Our Stk #: (\d+-\d+)/', $str, $matches)) {

You could do:

<?php

$str = "Microchip Technology Inc.
18 PIN, 7 KB FLASH, 256 RAM, 16 I/O 

Mfr's Part #: PIC16F648A-I/SO 
Our Stk #: 383-0408";

if (preg_match('/Our Stk #: (\d*-\d*)/', $str, $matches)) {
    echo $matches[1];
}

this works only if the number part you're looking for has always the form digits-digits. A more general solution, with any number and any amount of dashes is given by @Richard86 as another answer to your question.

Edit:

In order to avoid the case when no digits are around the dash, as @Richard86 said in a comment, the regular expresion should look like:

if (preg_match('/Our Stk #: (\d+-\d+)/', $str, $matches)) {
自此以后,行同陌路 2024-11-14 22:10:50

你可以使用:

if (preg_match( '/Our Stk #: ([0-9\\-]+)/', $str, $match ))
    echo $match[1];

You could use:

if (preg_match( '/Our Stk #: ([0-9\\-]+)/', $str, $match ))
    echo $match[1];
看轻我的陪伴 2024-11-14 22:10:50
$string = 'YOURSTRING';
$offset = strpos($string, 'Out Stk #') + 11;
$final = substr($string, $offset, 8);

如果我们不知道数字的长度,并且假设空白是数字后面的下一个字符,那么:

$string = 'YOURSTRING';
$offset = strpos($string, 'Out Stk #') + 11;
$end = strpos($string, ' ', $offset);
$final = substr($string, $offset, $end-$offset);
$string = 'YOURSTRING';
$offset = strpos($string, 'Out Stk #') + 11;
$final = substr($string, $offset, 8);

if we do not know the length of the number then and lets say whitespace is next character after the number, then:

$string = 'YOURSTRING';
$offset = strpos($string, 'Out Stk #') + 11;
$end = strpos($string, ' ', $offset);
$final = substr($string, $offset, $end-$offset);
ㄟ。诗瑗 2024-11-14 22:10:50
<?php

$text = "Microchip Technology Inc.
18 PIN, 7 KB FLASH, 256 RAM, 16 I/O 

Mfr's Part #: PIC16F648A-I/SO 
Our Stk #: 383-0408";

preg_match('/Our Stk #: (.*)/', $text, $result);
$stk = $result[1];

?>
<?php

$text = "Microchip Technology Inc.
18 PIN, 7 KB FLASH, 256 RAM, 16 I/O 

Mfr's Part #: PIC16F648A-I/SO 
Our Stk #: 383-0408";

preg_match('/Our Stk #: (.*)/', $text, $result);
$stk = $result[1];

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