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
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');
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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!!!!

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.