Remove spaces

I’m using the Advanced custom fields plugin for wordpress and would like to output a field value without any spaces. I need it without any spaces so that the anchor link works properly.

<li  role="presentation" class="NavTabs"><a href="#<?php $RouteName = the_sub_field('route_name');?> <?php $RoutName = preg_replace('/\s+/', '', $RouteName); echo $RouteName?>"	

Thx

Try this PHP function: str_replace(…)

http://php.net/str_replace

Not tested

$stringWithoutSpaces = str_replace( [" ", "%20"], "", $stringWithSpaces );

Hmm. That didn’t work. The funny thing is that the the_sub_field() function still out puts the value even if I remove

<?php $RoutName = preg_replace('/\s+/', '', $RouteName); echo $RouteName?>"

Another issue may be that is that $RoutName is a local variable?

I’m confused. In this bit of code:

<?php $RoutName = preg_replace('/\s+/', '', $RouteName); echo $RouteName?>

you create a variable called $RoutName which is the string without spaces, but then you echo the original $RouteName, not the copy without the spaces? Also there’s no semi-colon after the echo(), shouldn’t that throw an error message? (Note, I know nothing about Wordpress).

Well, I’m very new to php. I’m trying to figure this out get the return value of the advanced customs field function <?php the_sub_field('route_name')?> then remove all spaces from that string and echo the new value of the variable $RoutName with the spaces of the string removed.

OK, for me that would be

<li  role="presentation" class="NavTabs"><a href="#<?php $RouteName = the_sub_field('route_name');
$RoutName = preg_replace('/\s+/', '', $RouteName); 
echo $RoutName; ?>

… assuming that your preg_replace does what you want it to, or John_Betong’s version using str_replace() if you prefer.

<li  role="presentation" class="NavTabs"><a href="#<?php $RouteName = the_sub_field('route_name');
  $RoutName = str_replace( [" ", "%20"], "", $RouteName); 
echo $RoutName; ?>

Key thing in either is that you’re creating a new variable called $RoutName, which is a copy of $RouteName but passed through whatever function you choose to remove spaces. So you then have to echo that variable, not the original, or you’ll still get the spaces.

Thanks, but neither solution works.

Try taking the php script outside of the unordered list and introducing two additional list items. Also renaming $RoutName to $uriName to avoid confusion.

First list item is ‘XXX’ .$RouteName .‘XXX’ second list item is ‘YYY’ .$uriName .‘YYY’

Tapped laboriously from a tablet :frowning:

We’re going to need a bit more information then. When you say “neither works”, what exactly happens? Do you get error messages, the wrong link text, no link text? To start with, what’s in $RouteName and what’s in $RoutName? And can you show more of the code, a few extra lines to show your query, and the start and end of the loop that builds the list?

This is what I tried last

<li role="presentation" ><a href="#<?php $RouteName = the_sub_field('route_name'); $URIname = str_replace( [" ", "%20"],"", $RouteName ); echo $URIname; ?>" aria-controls="profile" role="tab" data-toggle="tab"><?php the_sub_field('route_name'); ?></a></li>

I believe that there is an error in this code, but I don’t know where. Dreamweaver is coloring the line of code red, but I can’t seem to find the error.

This is what is being output when I run the code

<a href="#Route 4" aria-controls="profile" role="tab" data-toggle="tab">Route 4</a>

This of course is what I want

<a href="#Route4" aria-controls="profile" role="tab" data-toggle="tab">Route 4</a>

[" “,”%20"] is not valid PHP declaration of an array.

str_replace( array(" ", "%20"),"", $RouteName );

Well, I tried this but I still get the same result

 <li role="presentation" ><a href="#<?php $RouteName = the_sub_field('route_name'); $URIname = str_replace( array(" ","%20"),"", $RouteName ); echo $URIname; ?>" aria-controls="profile" role="tab" data-toggle="tab"><?php the_sub_field('route_name'); ?></a></li>

Below that line, put:

<?php
var_dump($RouteName);
var_dump($URIname); ?>

What do you get from this?

I get

null
string ’ ’ (length=0)

So that tells me that the_sub_field(‘route_name’) isnt working.

It actually is working. It’s putting out this.

However, I want this.

I feel like it just ignores all of this code

$URIname = str_replace( array(" ", "%20"),"", $RouteName ); echo $URIname;

And you put those lines i gave you directly below the line you posted?

Can you insert this script before <ul>

<?php 
   
  $RouteName = the_sub_field( 'route_name' ); 

  echo '<pre>';
    var_dump( $_POST  );
    var_dump( $RouteName );
  echo '</pre>';
die;

This is what I did

   <li role="presentation" ><a href="#<?php $RouteName = the_sub_field('route_name'); $URIname = str_replace( array(" ", "%20"),"", $RouteName ); echo $URIname;?>" aria-controls="profile" role="tab" data-toggle="tab"><?php the_sub_field('route_name'); ?>
                                                   <?php var_dump($RouteName);
							var_dump($URIname); ?>
                                                   </a></li>

This is what I got when I did what you just said

array (size=0)
  empty
null