Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2010
    Location
    Beaverton, OR
    Posts
    38
    Thanks
    3
    Thanked 2 Times in 2 Posts

    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):
    -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:
    • 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.

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts

  • #3
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    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++;
    }
    ?>

  • Users who have thanked patryk for this post:

    chrisrozwod (10-14-2012)

  • #4
    New Coder
    Join Date
    Sep 2010
    Location
    Beaverton, OR
    Posts
    38
    Thanks
    3
    Thanked 2 Times in 2 Posts
    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:

    • 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.
    Last edited by chrisrozwod; 10-14-2012 at 01:33 AM. Reason: New info to consider!

  • #5
    New Coder
    Join Date
    Sep 2010
    Location
    Beaverton, OR
    Posts
    38
    Thanks
    3
    Thanked 2 Times in 2 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •