shamuraq Posted January 7, 2012 Share Posted January 7, 2012 What do u get if 1/2 + 1/3? How do i use preg replace to select the fractions part? I need to add html <strong> to bold only the fractions part. Is there regex involved? Quote Link to comment Share on other sites More sharing options...
shamuraq Posted January 7, 2012 Author Share Posted January 7, 2012 I tried this <?php $string = 'what do u get 1/2?'; $pattern = '/(\d+) (/) (\d+)/i'; $replacement = '$1 | $3'; echo preg_replace($pattern, $replacement, $string); ?> but i didn't work... Nothing came out Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2012 Share Posted January 7, 2012 Enable error reporting by setting the following directives in your php.ini file. Then restart Apache and see what errors you get. error_reporting = -1 display_errors = On Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 7, 2012 Share Posted January 7, 2012 <style type="text/css"> span.highlight {font-weight:bold;} </style> <?php $string = 'what do u get 1/2?'; $pattern = '/([0-9][\/][0-9])/'; echo preg_replace($pattern, '<span class="highlight">\1</span>', $string); ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted January 9, 2012 Share Posted January 9, 2012 @shamuraq a) the most immediate reason your preg_replace didn't work was because you have spaces between the part of your pattern that match for the numbers vs. the /, so it expects for instance "1 / 2" not "1/2". b) supposing your string does have a fraction with spaces in it like "1 / 2" your replacement basically just swaps out the "/" for a "|"...which..why? You said you wanted to mark it up with some html strong... @QuickOldCar: couple things about your regex.. a) since you are matching for a / it would be cleaner to pick a different pattern delimiter so you don't have to escape it in the pattern. b) [..] is a character class. It means to match any one thing inside the bracket. It is good for if you want to match for more than 1 thing like something in a range. For example [a-z0-9] will match any 1 letter or number. Since you are only matching for one thing (a "/"), while it *technically* works, there's no need for a character class. c) Your pattern will only match single-digit fractions. For instance, it will not match 1/10 (it will only match 1/1 part of it). @shamuraq: Here is a pattern for you: $pattern = '~([0-9]+\s?+/\s?[0-9]+)~'; This will capture the entire fraction $1 or \1 for your $replacement variable. This pattern will allow for both "1/2" and "1 / 2". If you only want it to match if no spaces, remove the two "\s?" parts. Quote Link to comment Share on other sites More sharing options...
shamuraq Posted January 12, 2012 Author Share Posted January 12, 2012 thanx .josh. Really appreciate it... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.