...

View Full Version : str_replace with exceptions? Not sure how to solve.



chrisrozwod
10-14-2012, 12:00 AM
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.

patryk
10-14-2012, 01:56 AM
check out preg_replace()
http://php.net/manual/en/function.preg-replace.php

patryk
10-14-2012, 02:17 AM
you can do it like that:


<?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++;
}
?>

chrisrozwod
10-14-2012, 02:29 AM
check out preg_replace()
http://php.net/manual/en/function.preg-replace.php

Thanks for replying. I'm afraid I need a bit more guidance, however.

If I do this

$c1r = preg_replace("/-/", "</li><li>", $row['c1r']);
or this:

$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.

chrisrozwod
10-14-2012, 02:48 AM
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
$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum