Jump to content

preg_replace remove ,0 or ,1 also remove first zero


CBG

Recommended Posts

I have the first problem fixed.

With it removing ,0 or ,1 by using

 

$str2 = $newstring;
$pattern2 = "{\,1||,0+}";
$newstring2 = preg_replace($pattern2,"",$str2);
echo '<br>';
echo $newstring2;

 

Now it just leaves removing the first 0 if it exists

This pattern will catch either ",0" or ",1":

<?php
$pattern2 = "{\,(0|1)+}";
?>

 

To trim off the zero at the beginning, you can use the ltrim() funtion:

<?php
$str = '0x.xx';
echo ltrim($str,'0');
?>

 

Ken

Thanks for the reply.

 

This is working to well

$str = '0x.xx';
echo ltrim($str,'0');

 

Let say it is 00.00 I am wanting to remove the first 0

Then again it could be 01.00 I want to remove the first 0

Or it could be 10.00 which mean there is no 0 to remove

 

The numbers could be anything.

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.