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;
?>
The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.
Use the String. replace() method to remove all whitespace from a string, e.g. str. replace(/\s/g, '') . The replace() method will remove all whitespace characters by replacing them with an empty string.
Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.
Approach 1: Using the str_replace() and str_split() functions in PHP. The str_replace() function is used to replace multiple characters in a string and it takes in three parameters. The first parameter is the array of characters to replace.
Try to add u-parameter for regex-pattern, because a string can have UTF-8 encoding:
$classname = preg_replace('/\s+/u', '', $fieldname);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With