PHP Knowledge Base

preg_replace Examples

php
<?php

$room_info__7_1 = '7_7-1';
$room_info__12_1 = '12_12-1';


$pattern0 = '/.*_/';
$pattern1 = '/_.*/';

$bed__7_1 = preg_replace($pattern0, '', $room_info__7_1);
$bed__12_1 = preg_replace($pattern0, '', $room_info__12_1);

$room__7_1 = preg_replace($pattern1, '', $room_info__7_1);
$room__12_1 = preg_replace($pattern1, '', $room_info__12_1);

?>
html
<p>
Bed number from '7_7-1': <?=$bed__7_1?><br>
Bed number from '12_12-1': <?=$bed__12_1?>
</p>

<p>
Room number from '7_7-1': <?=$room__7_1?><br>
Room number from '12_12-1': <?=$room__12_1?>
</p>
output
Bed number from '7_7-1': 7-1
Bed number from '12_12-1': 12-1 Room number from '7_7-1': 7
Room number from '12_12-1': 12