开发者

Selecting a part of a string

开发者 https://www.devze.com 2023-04-10 00:34 出处:网络
I have a string stored in variable say $input = 999 success: id:10.123/AVC13开发者_如何学Go231 | ark:/asf4523/2425fsaf

I have a string stored in variable say

$input = 999 success: id:10.123/AVC13开发者_如何学Go231 | 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?


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));


And the mandatory regex solution:

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


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...


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号