Portál AbcLinuxu, 15. prosince 2025 00:04
$text = preg_replace('/([a-z0-9]{2})([a-z0-9]{2})-([a-z0-9]{2})([a-z0-9]{2})-([a-z0-9]{2})([a-z0-9]{2})/', '\1:\2:\3:\4:\5:\6', $text);
Ale jak provest strtoupper() pouze u teto MAC adresy a ne u celeho textu?
Dekuji
$text = preg_replace('/([a-f0-9]{2})([a-f0-9]{2})-([a-f0-9]{2})([a-f0-9]{2})-([a-f0-9]{2})([a-f0-9]{2})/', '<span style="text-transform:uppercase;">\1:\2:\3:\4:\5:\6</span>', $text);
Ale tohle detem neukazujte
preg_replace_callback:
<?php
$subject = 'mac address a1b2-c3d4-e5f6';
$text = preg_replace_callback(
'/([a-z0-9]{2})([a-z0-9]{2})-([a-z0-9]{2})([a-z0-9]{2})-([a-z0-9]{2})([a-z0-9]{2})/',
function ($matches) {
return strtoupper($matches[0]);
},
$subject
);
$text = preg_replace('/([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})-([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})-([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})/', '\1:\2:\3:\4:\5:\6', $text);
echo $text // mac address A1:B2:C3:D4:E5:F6
?>
Za domácí úkol to můžeš vylepšit, udělat z toho jen jeden regexp a omezit ty výčty tak, aby neobsahovaly nic navíc.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.