Replace one character with another
5 posts by 2 authors in: Forums > CMS Builder
Last Post: August 30, 2009 (RSS)
I'm trying to replace any occurrence of a dash (-) in the contents of a field with a space and then print the field contents (with the spaces) and I've not been able to get it to work.
I'd ultimately like to replace any occurrence of a dash or an underscore with spaces.
Here's what I've tried to use so far based on some things that I found in the forum.
<?php foreach ($portfolio_imagesRecords as $record): ?>
<?PHP $record['group_code'] = preg_replace("/^\s*-/i", " ", $record['group_code'] ); ?>
<?php echo $record['group_code'] ?>
<?php break ?>
<?php endforeach; ?>
Anyone who can point me in the right direction?
Thanks,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Replace one character with another
By Chris - August 29, 2009
Here's the code you're looking for:
$record['group_code'] = preg_replace("/[-_]/", " ", $record['group_code'] );
/[-_]/ means "match a single character in the set [-_]". preg_replace() will repeat the match as many times as it can.
/^\s*-/i means "match from the start of the string, allowing any number of whitespace chatacters (i.e. tabs, spaces, newlines), then a single - character, without respect to case".
Regular expressions can be very tricky.
Hope this helps! Please let us know if you have any more questions or comments! :)
Chris
Re: [chris] Replace one character with another
By gkornbluth - August 29, 2009 - edited: August 29, 2009
Thanks!!!
And thanks for the explanation as well.
So if I wanted to replaced any dashes or underscores with spaces as well, would I add the entire snippet again, including the comma?
"/[__]/", "/[-_]/",
Also, how would I replace a dash with a space, and an underscore with a star (just to demonstrate the replacement of 2 (or more) characters with other specific characters?
Thanks again, and again, and....
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Replace one character with another
By Chris - August 29, 2009
The first argument (before the comma) is the pattern to match; the second argument is what the match should be replaced with.
$record['group_code'] = preg_replace("/-/", " ", $record['group_code'] );
The above code will replace any occurrence of a dash ("-") in the string with " ".
$record['group_code'] = preg_replace("/[-_]/", " ", $record['group_code'] );
The above code will replace any occurrence of either a dash ("-") or an underscore ("_") in the string with " ".
$record['group_code'] = preg_replace("/-/", " ", $record['group_code'] );
$record['group_code'] = preg_replace("/_/", "*", $record['group_code'] );
The above code will replace any occurrence of a dash ("-") in the string with a space (" "). Then it will replace any occurrence of an underscore ("_") with an asterisk ("*").
Hope this helps! You might want to read a chapter or two on regular expressions if you want to learn how this stuff all works. :)
Chris
Re: [chris] Replace one character with another
Thanks for the explanation.
It helps to understand and not just paste some code.
Best,
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php