14

Hi, I am getting a PHP string which I need to strip the spaces out of. I have used the following code but when I echo $classname it just displays the string still with the spaces in it.

   <?php
     $fieldname = the_sub_field('venue_title');
     $classname = str_replace(' ', '', $fieldname);
     echo $classname;
   ?>
w5m
  • 2,286
  • 3
  • 34
  • 46
Paul Elliot
  • 475
  • 2
  • 6
  • 21
  • Use an hex editor to view the bytes, if the spaces aren't `20` in HEX then it's not a "normal" space. You may use a simple function in PHP to convert to HEX. – HamZa May 15 '13 at 11:15
  • possible duplicate of [To strip whitespaces inside a variable in PHP](http://stackoverflow.com/questions/1279774/to-strip-whitespaces-inside-a-variable-in-php) – Léo Léopold Hertz 준영 Mar 25 '14 at 19:02

7 Answers7

34

Try to add u-parameter for regex-pattern, because a string can have UTF-8 encoding:

$classname  =  preg_replace('/\s+/u', '', $fieldname);
T.Todua
  • 53,146
  • 19
  • 236
  • 237
Alekzander
  • 866
  • 3
  • 12
  • 12
16

If you know the white space is only due to spaces, you can use:

$classname = str_replace(' ','',$fieldname ); 

But if it could be due to space, you can use:

$classname = preg_replace('/\s+/','',$fieldname )
Vijaya Pandey
  • 4,252
  • 5
  • 32
  • 57
  • 1
    Try substituting the `\s+` for `\h+` for horizontal whitespace, this will retain line breaks, if needed. – Martin Mar 16 '17 at 20:29
5

The problem might be the character not being a space, but another whitespace character.

Try

$classname = preg_replace('/\s/', '', $fieldname);
ty812
  • 3,293
  • 19
  • 36
3

use trim like this

TRIM($fieldname);

EDIT:

preg_replace('/\s+/', '', $fieldname);
Amir
  • 4,089
  • 4
  • 16
  • 28
  • 2
    `trim()` will only remove whitespace from the start and end of the string – billyonecan May 15 '13 at 11:10
  • 'trim()' only trims at the beginning or the end of a string. In Paul's example, whitespace characters might be within the string. – ty812 May 15 '13 at 11:11
2

It could be that the space is not really a space, but other form of whitesspace.

You might want to try:

$classname = preg_replace('/\s+/', '', $fieldname);

From here.

Community
  • 1
  • 1
2

The issue was the field that it was pulling, not the rest of the php. 'the_sub_field('venue_title')' pulls a field from the wordpress plugin 'Advanced Custom Fields' but this function is intended to display the data rather than just retrieve it. Instead i used 'get_sub_field('venue_title')' and it worked. cheers for the help

Paul Elliot
  • 475
  • 2
  • 6
  • 21
0
<?php
  $fieldname = "I  am  21  Years  Old";
  $classname = str_replace(' ', '', $fieldname);
  echo $classname;
?>

This runs perfectly. Check value return by this function: the_sub_field('venue_title');

Blu
  • 4,036
  • 6
  • 38
  • 65
  • I think the issue is that the string is not getting treated as a string. it needs a " on either side. how would you do this in php? eg $fieldname = "'" . the_sub_field('venue_title') . "'"; – Paul Elliot May 15 '13 at 11:27
  • use " ". the_sub_field('venue_title')."" or maybe ""+the_sub_field('venue_title')+"" – Blu May 15 '13 at 11:39