Jump to content

Removing line breaks


V

Recommended Posts

I'm trying to implement a php code in a Wordpress site to remove line breaks from excerpts. I suspect str_replace will work but I'm not sure how to use it with a function..

 

<?php the_excerpt(); ?>

 

anyone have any idea?

 

 

Link to comment
Share on other sites

Well it all depends on what the function does.

 

Does it echo or return the statement as a value? Given that it does not have an echo preceding it, I am taking that it echos. And because of this you would either need to use output buffering, change the function to return a value, or add a parameter to tell it to echo it without line breaks. (via an if statement).

Link to comment
Share on other sites

I'm trying to implement a php code in a Wordpress site to remove line breaks from excerpts. I suspect str_replace will work but I'm not sure how to use it with a function..

 

<?php the_excerpt(); ?>

 

i think you havta use javascript on this one. This echos it, not returns it

 

anyone have any idea?

 

 

Link to comment
Share on other sites

As premiso said, it depends. If it returns the excerpt something like this will do:

 

echo str_replace("\n", '', the_excerpt());

 

If it echos the excerpt something like this will work:

 

ob_start();
the_excerpt();
echo str_replace("\n", '', ob_get_clean());

Link to comment
Share on other sites

Thank you for the codes AlexWD :) For some reason they don't remove the <br> tags. Not sure if /n in the same but I tried using <br> in the code as well. Maybe I have to go into the wordpress function file and try from there.

Link to comment
Share on other sites

<br> is an html line break. If you wanted to remove those instead use:

 

str_replace("<br>", '', ...);

 

Or if you want to remove both literal and html line breaks:

 

str_replace(array("\n", '<br>'), '', ...);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.