1

Hi I am echoing out a field name which is going to be part of a url. However the name itself has uppercase lettyers in it so I have made them all lower on the echo. There is however a space between words. Is there a way I can also replace any spaces here with a hiphen on the echo?

<?php echo strtolower($row["myfield"]); ?>
Dharman
  • 26,923
  • 21
  • 73
  • 125
PeterT
  • 39
  • 5

3 Answers3

2

You can simply replace the spaces with another string:

<?php echo str_replace(' ', '-', strtolower($row["myfield"])); ?>

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

Documentation: https://secure.php.net/manual/en/function.str-replace.php

T K
  • 598
  • 7
  • 19
0
<?php
 echo strtolower(str_replace(" ","-",$row["myfield"]));

Based on the comments below. This code does two operation, first takes "myfield" and replatces the white space with hyphens, it does that using the str_replace() function of php. This function returns a string that is then used with conjuction with strtolower to make all letters lower.

thefolenangel
  • 800
  • 5
  • 25
0

For just spaces, use str_replace:

   <?php echo str_replace(' ', '',strtolower($row["myfield"])); ?>

For all whitespace, use preg_replace:

  <?php echo  preg_replace('/\s+/', '', strtolower($row["myfield"])); ?>
Pankaj Yadav
  • 163
  • 1
  • 7