Jump to content

PHP regular expression


freephoneid

Recommended Posts

Hi,

  Can anyone tell me how to convert a given string in the below format in 1 shot in PHP?

 

Given String: MT9L2BGQGJC547VN4JT4Z8WY9FKFGZC

 

Expected: MT-9L2B-GQGJC-547VN-4JT4Z-8WY9F-KFGZC

 

Can anyone tell me how to do it using better way may be using Regualr expression in PHP.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/116498-php-regular-expression/
Share on other sites

There's probably an easier way, but....

 

<?php
function format($str)
{
$str = preg_split('%%', $str, -1, PREG_SPLIT_NO_EMPTY);
$newstr = '';

$length = count($str);
for($i = 0; $i < $length; ++$i)
{
	$char = array_shift($str);
	if($i == 2 || $i == 6 || ((count($str) + 1) % 5 == 0 && $i != 1))
	{
		$newstr .= '-' . $char;
	}
	else {
		$newstr .= $char;
	}
}
return $newstr;
}

echo format('MT9L2BGQGJC547VN4JT4Z8WY9FKFGZC');
?>

Hi,

  Thanks for the great code...but if I enter "AFDSGDGDK", it gives wrong result....it should give "AF-DSGD-GDK"

 

The format is 2 Chars - 4  Chars - 5 chars - 5 Chars - 5 chars - 5 chars - 5 chars

 

Can you tell me how to fix that?

 

Thanks again!

 

Ah, thought you meant there was a set length of characters (what you had originally). It won't work for any character lengths different than what you put earlier. Unfortunately I don't have the time to fix it to work for any string length right now.

try this:

<pre>
<?php
$str = 'MT9L2BGQGJC547VN4JT4Z8WY9FKFGZC';
$str1 = substr($str,0,2);
$str2 = substr($str,2,4);
$str3 = substr($str,6,5);
$str4 = substr($str,11,5);
$str5 = substr($str,16,5);
$str6 = substr($str,21,5);
$str7 = substr($str,26,5);
echo $str1."-".$str2."-".$str3."-".$str4."-".$str5."-".$str6."-".$str7;
?>
</pre>

<pre>
<?php	
$tests = array(
	'MT9L2BGQGJC547VN4JT4Z8WY9FKFGZC',
	'AFDSGDGDK',
);
foreach ($tests as $test) {
	if (!preg_match('/\A(.{2})(.{4})(.+)/', $test, $pieces)) {
		die('Invalid format');
	}
	array_shift($pieces);
	$pieces[2] = join(
		'-',
		preg_split(
			'/(.{5})/',
			$pieces[2],
			-1,
			PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
		)
	);
	echo join('-', $pieces);
	echo '<hr>';

}
?>
</pre>

Thanks Everyone!!!!!!!!!!

 

This Forum is GREAT!!!!!!!

 

Can you also explain a little what the below line does exactly?

 

preg_match('/\A(.{2})(.{4})(.+)/', $str, $pieces); ---> Here, what is A stands for?

 

preg_split('/(.{5})/', $pieces[2], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); ---> Here, what is PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY??

 

Thanks!!!!

 

 

Hi effigy,

  I just tried your code & unfortunately it does not work for 2 characters string input. If I enter "GH", then ideally it should return "GH" only but its not working...

 

  Can anyone fix it as well??

 

  Appreciate your help & thanks again for all resopnses.

 

Thanks!

Finally got time to fix my old code now...

 

<?php
$str = 'ALDABSDDFEFE';

$strLength = strlen($str);

$newstr = substr($str, 0, 2);
if($strLength > 2)
{
$newstr .=  '-' . substr($str, 2, 4);
}
if($strLength > 6)
{
$remainingStr = substr($str, 6);
$remainingStr = preg_split('%%', $remainingStr, -1, PREG_SPLIT_NO_EMPTY);

$remainingChars = count($remainingStr);
for($i = 0; $i < $remainingChars; ++$i)
{
	$char = array_shift($remainingStr);
	if($i % 5 == 0)
	{
		$newstr .= '-' . $char;
	}
	else {
		$newstr .= $char;
	}
}
}

echo $newstr;
?>

try

<?php
$test = 'MT9L2BGQGJC547VN4JT4Z8WY9FKFGZC';
$part = array();
$part[] = substr($test,0,2);
$test =substr($test,2);
$part[] = substr($test,0,4);
$test =substr($test,4);
while (strlen($test)){
$part[] = substr($test,0,5);
$test =substr($test,5);
}
echo $out = implode('-', $part);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.