Jump to content

preg_replace remove ,0 or ,1 also remove first zero


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.

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.