Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Replacing spaces for hyphens in a custom field
I looking for a way to output a custom field within an article, but which would replaces spaces with hyphens. So if “this was the value” in the custom field, it would be output as “this-was-the-value”
I’m guessing that I’ll have to use php, but I’ve no idea where to start. Can someone point me in the right direction ?
~Nick
Offline
Re: Replacing spaces for hyphens in a custom field
By looking at the syntax of two other php scripts, I put together this:
<code>
<txp:php>
$c = custom_field( array(“name”=>“CUSTOMFIELDNAMEGOESHERE”))
$oldWord = “ “;
$newWord = “-”;
$c = str_replace($oldWord , $newWord , $c);
echo $c;
</txp:php>
</code>
Seems to work fine for me.
~Nick
Offline
#3 2006-07-25 16:29:54
- Ace of Dubs
- Member

- Registered: 2006-04-17
- Posts: 446
Re: Replacing spaces for hyphens in a custom field
Neat trick there Nick.
I too am starting to get my feet wet in PHP..
WHere did you find those scripts?
Offline
Re: Replacing spaces for hyphens in a custom field
One came from these forums, (I’ve looked and can’t find the original article) which I already use to mung a custom field containing an email address:
<code>
<txp:php>
$c = custom_field( array(“name”=>“CUSTOMFIELDNAME”));
$a = array(“email” => $c, “linktext”=> “Click here to contact the photographer”, “title” => “Email the photographer” );
echo email ( $a );
</txp:php>
</code>
but most came from a Google search:
<code>
<php
// The text string
$text = “The quick brown fox jumped over the lazy dog.”;
// The word we want to replace
$oldWord = “brown”;
// The new word we want in place of the old one
$newWord = “blue”;
// Run through the text and replaces all occurrences of $oldText
$text = str_replace($oldWord , $newWord , $text);
// Display the new text
echo $text;
?>
</code>
~Nick
Last edited by NickML (2006-07-26 09:26:18)
Offline