0

I am trying to remove blank spaces at the beginning of the_excerpt();. They are cuased by blanc spaces between two images in the content. I have unsuccessfully tried to get rid of them by using str_replace, in the code below.

$string = get_the_excerpt();
$string = str_replace(' ','',$string);
echo $string;

With the same setup it is possible to remove normal spacing

$string = get_the_excerpt();
$string = str_replace(' ','',$string);
echo $string;

Why is this and how may I remove the  ?

UPDATE

This is how the images are placed in the editor - it is the space between the images causing the   at the beginning of the excerpt. This is how the images are placed in the editor - it is the space between the images causing the   at the beginning of the excerpt. .

This is the result

This is the result

This is what it looks like when I inspect the elements. This is what it looks like when I inspect the elements.

3
  • This is more a php question in general than wp specific.
    – cjbj
    Jun 14, 2016 at 10:52
  • Why not to just write an excerpt instead of fighting one bug at a time? Jun 14, 2016 at 12:50
  • Yes perhaps @cjbj but it is working fine with a regular string as in examples but not when I retrieve the excerpt? Because it will be a reoccurring problem @MarkKaplun!
    – HannesH
    Jun 14, 2016 at 18:35

1 Answer 1

1

Try ltrim() to remove characters at the begining of a string.

$str = '        Some Stuff here   this is ok and     this is';

echo ltrim(ltrim($str, ' '), ' ');
6
  • Thanks for your reply, this works with your example but not with the excerpt.. @jgraup
    – HannesH
    Jun 14, 2016 at 18:32
  • Can you paste the full string so it's possible to test with your data?
    – jgraup
    Jun 14, 2016 at 18:33
  • I have updated with some images to explain how I set up the string @jgraup
    – HannesH
    Jun 14, 2016 at 18:52
  • I see, so it's a mix until you hit a non-space char. Easy enough but I won't have time today to update this.
    – jgraup
    Jun 14, 2016 at 18:56
  • Actually, just replace 'nsbsp' with ' ' before l trim. Normalizing it will help.
    – jgraup
    Jun 14, 2016 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.