Jump to content

Would like help with line breaks and str_replace.


Recommended Posts

EDIT: There's HTML in what I typed, so it's all screwy. I guess... Quote my post to see what I actually said?

 

I have a thing where it takes input, and formats it with HTML so that linebreaks are shown. I've got it so that it'll take every line break and place a <br /> on it (but not get rid of the line break). That became a problem, because every time I edited it, the line breaks would double, but I fixed it by making it replace <br /><br /> with just one <br />. So that's working.

 

But I still want to know how I can actually get rid of it, because it's bothering me, the way <br /> is placed at the beginning of each newline. I want it to be at the end. A backspace thing (like \b maybe?) would be really useful. I can't find it, though. So I'm probably just making it up. Am I?

 

This is my code:

$convertthis = array('
','<p><p>','</p></p>','<br /><br />');
$tothis = array('<br />','<p>','</p>','<br />');
$edit1 = substr_replace($_POST['blurb'],"<p>",0,0); //beginning of string
$edit2 = substr_replace($edit1,"</p>",strlen($edit1),0); //end of string
$blurb = str_replace($convertthis,$tothis,$edit2); //replacing line breaks, getting rid of doubles

 

(Yeah, I put actual line breaks in $convertthis. I tried to make it search for '\n\n' but for some reason it wouldn't find it. So, just a by-the-way question... is this normal?)

If you want to convert line breaks to html line breaks then use nl2br()- It does it all for you.

 

Also you should only run that code when you get it out of the database. Do not run this code when adding the data into the database. That way when you go to edit it later it wont duplicate the line breaks. Keep everything in its raw state when being stored in the database Only do the syntax formatting later on.

 

Also note when posting html code in posts use the [nobbc][/nobbc] tags. I have edited your post with this addition.

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.