Replace one character with another

5 posts by 2 authors in: Forums > CMS Builder
Last Post: August 30, 2009   (RSS)

Well, I'm stumped again.

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", "&nbsp;", $record['group_code'] ); ?>

<?php echo $record['group_code'] ?>

<?php break ?>

<?php endforeach; ?>


Anyone who can point me in the right direction?

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







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

Hi Jerry,

Here's the code you're looking for:

$record['group_code'] = preg_replace("/[-_]/", "&nbsp;", $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! :)
All the best,
Chris

Re: [gkornbluth] Replace one character with another

By Chris - August 29, 2009

Hi gkornbluth,

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("/-/", "&nbsp;", $record['group_code'] );

The above code will replace any occurrence of a dash ("-") in the string with "&nbsp;".

$record['group_code'] = preg_replace("/[-_]/", "&nbsp;", $record['group_code'] );

The above code will replace any occurrence of either a dash ("-") or an underscore ("_") in the string with "&nbsp;".

$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. :)
All the best,
Chris

Re: [chris] Replace one character with another

Chris,

Thanks for the explanation.

It helps to understand and not just paste some code.

Best,

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php