选择字符串的一部分

发布于 2024-12-07 12:44:47 字数 325 浏览 3 评论 0原文

我有一个存储在变量中的字符串,假设

$input = 999 success: id:10.123/AVC13231 | ark:/asf4523/2425fsaf

我只想选择字符串“10.123/AVC13231”的一部分,

假设我想实现此目的:

$output = 10.123/XXXXXXXX ; 

并且不应选择其他部分 $input,即使是 id: 部分

值 10.123 是常量并且AVC13231 的值动态变化。

我怎样才能实现上述目标?

I have a string stored in variable say

$input = 999 success: id:10.123/AVC13231 | ark:/asf4523/2425fsaf

I want to select only a part of a string "10.123/AVC13231"

say i want to achieve this:

$output = 10.123/XXXXXXXX ; 

and no other part $input should be selected even the id: part

The value 10.123 is constant and the value AVC13231 changes dynamically.

How can i achieve the above?

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

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

发布评论

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

评论(4

往事随风而去 2024-12-14 12:44:47

这是一个解决方案。

$input = "999 success: id:10.123/AVC13231 | ark:/asf4523/2425fsaf";
$pos1 = strpos($input, 'id:')+3;    // Remove 'id:'
$pos2 = strpos($input, '|')-1;      // Remove space before pipe
$output = substr($input, $pos1, ($pos2 - $pos1));

Here's a solution.

$input = "999 success: id:10.123/AVC13231 | ark:/asf4523/2425fsaf";
$pos1 = strpos($input, 'id:')+3;    // Remove 'id:'
$pos2 = strpos($input, '|')-1;      // Remove space before pipe
$output = substr($input, $pos1, ($pos2 - $pos1));
野却迷人 2024-12-14 12:44:47

以及强制正则表达式解决方案:

preg_match("/id:([^\s]*)/", $input, $matches);
$output = $matches[1];

And the mandatory regex solution:

preg_match("/id:([^\s]*)/", $input, $matches);
$output = $matches[1];
棒棒糖 2024-12-14 12:44:47

您还可以使用:

$data = substr($input, $startpos=(strpos($input, "id:")+3), strpos($input, ' ', $startpos)-$startpos);

未测试,但逻辑是存在的,只需正确调整算法...

You could also use:

$data = substr($input, $startpos=(strpos($input, "id:")+3), strpos($input, ' ', $startpos)-$startpos);

Not tested, but the logic is there, just adapt correctly the algorithm...

柠栀 2024-12-14 12:44:47

试试这个

$input = "999 success: id:10.123/AVC13231 | ark:/asf4523/2425fsaf";

$first_split=explode(" |",$input);
$input_split1=$first_split[0];
$second_split=explode("10.123",$input_split1);
$input_split2=$second_split[1];
$output="10.123".$input_split2;

echo $output;

Try this

$input = "999 success: id:10.123/AVC13231 | ark:/asf4523/2425fsaf";

$first_split=explode(" |",$input);
$input_split1=$first_split[0];
$second_split=explode("10.123",$input_split1);
$input_split2=$second_split[1];
$output="10.123".$input_split2;

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