<?php

$x='xffx etgxrtgy hyh[pageid:2] t пепкеп пе
[pageid:100|a]]
exffxergtg rtg' ;

echo preg_replace_callback('/\[pageid:(\d+)(?:\|(\w+))?\]/',function($matches){
    return "{found:{$matches[1]} {$matches[2]} }";
},$x);