Replace & with and

Hi,

I have the following PHP code which creates a class to display a category name in lowercase and removes spaces with a hyphen. The issue i have is that some categories have an “&” in, for example: “sports-&-recreation”.

Is there a way I can replace the “&” with the word “and” so it appears “sports-and-recreation”?

$category_name = str_replace(’ ', ‘-’, strtolower($subterms->name));

                           $output .= '<div class="category-item">
  				 <span class="'. $category_name .'"></span></div>';

Thanks

You could probably use some code similar to this line that you already have:

$category_name = str_replace(' ', '-', strtolower($subterms->name));
1 Like

Thanks, I’ve tried the following, but no luck:

$category_name = str_replace(' ', '-', '&', 'and', strtolower($subterms->name));
and
$category_name = str_replace(' ', '-', '"&"', 'and', strtolower($subterms->name));

What about looking at the documentation for the str_replace() function? You could pass in arrays, or just call the function twice, but you can’t just add a couple of extra parameters as you have done.

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

1 Like

The str_replace function will take arrays as parameters, that’s how you get multiple values for search and replace. You can’t add more parameters like that.

1 Like

Thanks for the replies.

I’ve now dot this:

$cat_name  = $subterms->name;
$amp = array("&amp;");
$and   = array("and");
$category_name = str_replace($amp, $and, strtolower($cat_name) );

But I can’t work out how to replace the spaces with a hyphen.

I’ve tried:

$cat_name  = $subterms->name;
$amp = array("&amp;");
$and   = array("and");
$category_name = str_replace($amp, $and, ' ', '-', strtolower($cat_name) );

But it didn’t work

Add them to the arrays. you are still adding extra paramters there.

1 Like

Thanks, it worked :slight_smile:

$cat_name  = $subterms->name;
$amp = array(" &amp; ");
$and   = array("-and-");
$category_name = str_replace($amp, $and, strtolower($cat_name) );

Not exactly what I had in mind.
The main point of arrays is that they can hold a number of different values.
Using single values there is no need to make them arrays.

How would you add it to the array?

I also have an issue with categories that don’t have the “&” in, so it’s creating 2 classes.

For examples:

<span class="poem listings"></span>
instead of
<span class="poem-listings"></span>

$search = array('&amp;', ' ');
$replace = array('and', '-');

Got it?

1 Like

If you look at the functions signature under description in the documentation
https://www.php.net/manual/en/function.str-replace.php
you’ll see

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

The square brackets [, int &$count ] mean the parameter is optional. The other 3 are not optional and there are only 3 of them.

What mixed means in mixed $search , mixed $replace , mixed $subject is not so clear, but some clue is under “Parameters”. The documentation has “value … an array may be used” but might be clearer if it were “string value … an array of string values may be used” instead.

What the last “mixed” means is

Return Values

This function returns a string or an array with the replaced values.

There are several examples under, errmm, Examples that should help you see how you should code what you want.

1 Like

Thank you, the above solution worked. I will look at the documentation.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.