Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-13-2012, 11:00 PM   PM User | #1
chrisrozwod
New Coder

 
Join Date: Sep 2010
Location: Beaverton, OR
Posts: 35
Thanks: 3
Thanked 2 Times in 2 Posts
chrisrozwod is an unknown quantity at this point
str_replace with exceptions? Not sure how to solve.

So far I've only been able to find partial solutions that produce a different problem. I'd really appreciate any help.

I started a would you rather site with my friends. They had dozens of questions written in word that they have copied and pasted into a database. Each question has four parts (two for each choice). The about and the rules and restrictions.

The trouble is that within each part, the information looks like this example from one of the rules and restrictions (I know it's dumb, please ignore the content. I chose this one because it has all the troublesome elements):
Quote:
-Elbow sucking must be consensual. In other words, the suckee must not oppose. -The elbow sucking does not count if you tell the person that you will die or will be harmed if they do not agree. -You cannot pay off someone to suck their elbow. -You can never re-suck the same person again.
The spaces between the period and hyphen are line breaks (actually spaces here because it was recognizing the line breaks, but you get the idea).

I would like it to look like this:
Quote:
• Elbow sucking must be consensual. In other words, the suckee must not oppose.
• The elbow sucking does not count if you tell the person that you will die or will be harmed if they do not agree.
• You cannot pay off someone to suck their elbow.
• You can never re-suck the same person again.
I tried a str_replace for the hyphens, but then it changes the hyphen in re-suck, which I don't want. I also can't str_replace the hyphen with a space after it because re-suck is a problem again. I can't put breaks only after the period because sometimes there are multiple sentences within each thing.

So far the best solution I've found is nl2br(), but that's still not great. I suppose the ideal situation would be to create list items.

I'm thinking if we could replace the hyphens that aren't between two characters with a "</li><li>" it might work. Obviously, I'd put an <li> and </li> around the string. It'd have to not run on the first instance, but still remove the hyphen, however, so that's a lot of exceptions. I assume it's doable, but I have no idea how.
__________________
http://linkedplay.com
chrisrozwod is offline   Reply With Quote
Old 10-14-2012, 12:56 AM   PM User | #2
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
check out preg_replace()
http://php.net/manual/en/function.preg-replace.php
patryk is offline   Reply With Quote
Old 10-14-2012, 01:17 AM   PM User | #3
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
you can do it like that:
PHP Code:
<?php
$data 
'-Elbow sucking must be consensual. In other words, the suckee must not oppose. -The elbow sucking does not count if you tell the person that you will die or will be harmed if they do not agree. -You cannot pay off someone to suck their elbow. -You can never re-suck the same person again.';

$pieces explode(' -'$data);
$i 0;
while(isset(
$pieces[$i])){
    
$pieces[$i] = preg_replace('/^[- ]/'''$pieces[$i]);
    if(
$pieces[$i] != ''){
        echo 
'<li>' $pieces[$i] . '</li>';
    }
    
$i++;
}
?>
patryk is offline   Reply With Quote
Users who have thanked patryk for this post:
chrisrozwod (10-14-2012)
Old 10-14-2012, 01:29 AM   PM User | #4
chrisrozwod
New Coder

 
Join Date: Sep 2010
Location: Beaverton, OR
Posts: 35
Thanks: 3
Thanked 2 Times in 2 Posts
chrisrozwod is an unknown quantity at this point
Quote:
Originally Posted by patryk View Post
Thanks for replying. I'm afraid I need a bit more guidance, however.

If I do this
PHP Code:
$c1r preg_replace("/-/""</li><li>"$row['c1r']); 
or this:
PHP Code:
$c1r str_replace("-""</li><li>"$row['c1r']); 
I wind up with this:
Quote:

• Elbow sucking must be consensual. In other words, the suckee must not oppose.
• The elbow sucking does not count if you tell the person that you will die or will be harmed if they do not agree.
• You cannot pay off someone to suck their elbow.
• You can never re
• suck the same person again.
I'm not sure how to remove the first one by doing it that way. I also don't know how to remove the hyphen in re-suck. I didn't see those things outlined in the link.


Okay, hang on. Gotta check out that second message. Replied before I saw that one.
__________________
http://linkedplay.com

Last edited by chrisrozwod; 10-14-2012 at 01:33 AM.. Reason: New info to consider!
chrisrozwod is offline   Reply With Quote
Old 10-14-2012, 01:48 AM   PM User | #5
chrisrozwod
New Coder

 
Join Date: Sep 2010
Location: Beaverton, OR
Posts: 35
Thanks: 3
Thanked 2 Times in 2 Posts
chrisrozwod is an unknown quantity at this point
YES!!!

I had to change the line breaks to spaces, but once I figured that out, it worked perfectly. Man oh man.

Wound up like this:
PHP Code:
<?php 
$data 
=  str_replace(array("\r\n""\n","\r"), ' '$row['c1r']); 

$pieces explode(' -'$data); 
$i 0
while(isset(
$pieces[$i])){ 
    
$pieces[$i] = preg_replace('/^[- ]/'''$pieces[$i]); 
    if(
$pieces[$i] != ''){ 
            echo 
'<li>' $pieces[$i] . '</li>'
    } 
$i++; 

?>
Thanks so much.
__________________
http://linkedplay.com
chrisrozwod is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:56 PM.


Advertisement
Log in to turn off these ads.