+2 votes
by
There is a line "[id875783012|Alex]". How can I use preg_match to get "id875783012" ?
by
Well, at least write down how you tried

1 Answer

0 votes
by
 
Best answer
If you know for sure that the id consists of digits, the easiest way is to delete all the non-digits from the string

$string = "[id875783012|Alex]";

$string = preg_replace('/[\D]+/', '', $string);
$string = 'id' . $string;

var_dump($string);
With preg_match() So:
$string = "[id875783012|Alex]";

$regexp = "/id(?<id>[\d]+)\|/";
$reglink = preg_match($regexp, $string, $match);
$id = $match['id'];

var_dump($id);
...