tags:

views:

622

answers:

4

im new to regular expressions in php.

I have some data in which some of the values are stored as zero(0).What i want to do is to replace them with '-'. I dont know which value will get zero as my database table gets updated daily thats why i have to place that replace thing on all the data.

$r_val=preg_replace('/(0)/','-',$r_val);

The code im using is replacing all the zeroes that it finds for eg. it is even replacing zero from 104.67,giving the output 1-4.56 which is wrong. i want that data where value is exact zero that must be replaced by '-' not every zero that it encounter. Can anyone please help!!

Example of the values that $r_val is having :-

10.31, 391.05, 113393, 15.31, 1000 etc.

+3  A: 

This depends alot on how your data is formatted inside $r_val, but a good place to start would be to try:

$r_val = preg_replace('/(?<!\.)\b0\b(?!\.)/', '-', $r_val);

Where \b is a 0-length character representing the start or end of a 'word'.

Strange as it may sound, but the Perl regex documentation is actually really good for explaining the regex part of the preg_* functions, since Perl is where the functionality is actually implemented.

Matthew Scharley
I wasn't sure if \b applied with numbers, apparently it does though. :) An alternative pattern could use a combination of look ahead and look behind assertions. '#(?<!\d)0(?!\d)#'
joebert
+1  A: 

Again, it would be more than helpful if you could supply an example of what the $r_val string really looks like.

Note that \b matches at word boundaries, which would also turn a string like "0.75" into "-.75". Not a desirable result, I guess.

Geert
ya you are right its replacing that o which i dont want.i have edited my question you can look there for examples of the values that $r_val is having
developer
+1  A: 

Whilst the other answer does work, it seems overly complex to me. I think you need only to use the ^ and $ chars either side of 0.

$r_val = preg_replace('/^0+$/', '&#45', $r_val);
  • ^ indicates the regex should match from the beginning of the line.
  • $ indicates the regex should match to the end of the line.
  • + means match this pattern 1 or more times

I altered the minus sign to it's html code equivalent too. Paranoid, yes, but we are dealing with numbers after all, so I though throwing a raw minus sign in there might not be the best idea.

MatW
hey i didnt knew this as im new to regex and all.Thanks for this code Matw =)
developer
A: 

Why not just do this?


if ( $r_val == 0 )
    $r_val = '-';

You do not need to use a regex for this. In fact, I'd advise against doing so for performance reasons. The operation above is approximately 20x faster than the regex solution.

Also, the PHP manual advises against using regexes for simple replacements:

If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace().

http://us.php.net/manual/en/function.str-replace.php

Hope that helps!

Mike
$r_val != 0 though, from what he's said it's more likely to hold "5.0, 34.05, 23.4"
Matthew Scharley
He said:"I have some data in which some of the values are stored as zero(0).What i want to do is to replace them with '-'"So the ones that are stored as 0 ( that is, $rval == 0 ) get changed to '-', while the others get left alone. No regex needed.
Mike
I agree, if this is the case, but... if it is, then why is he asking about regex in the first place? Surely people learn $rval == 0 before learning that regex exist...
Matthew Scharley
Maybe someone suggested he use a regex and then he came here for help. I don't know.Given his original question, though, my solution is sufficient and faster. If there are other requirements he hasn't mentioned, then that might change.
Mike
Well mike i dont have any other requirements and the code that you gave in your solution is exactly what i was doing earlier but i was told that its better to use regex soi went for regex.I didnt knew that this simple code is faster than regex.Thanks for info man!!
developer