Click to See Complete Forum and Search --> : [RESOLVED] help with str_replace please


j-dearden
09-27-2006, 12:26 PM
im having a slight problem with a little scenario. i got most of it working but need a little help.

the problem is this;

in my database i have a table called product description, the description consists of both paragrapths and lists, so when i entered the description into my table i placed a ; where the bullet point should be. the purpose being when i retreive this information using php i use the str_replace function to replace any occurances of ; with <li><span class='productDescription'>. this does display what i need in a list correctly the problem i am having is placing </li></span> at the end of each list element,

something like

<li><span class='productDescription'>item1</span></li>
<li><span class='productDescription'>item2</span></li>
<li><span class='productDescription'>item3</span></li>

the following code displays

<li><span class='productDescription'>item1
<li><span class='productDescription'>item2
<li><span class='productDescription'>item3

the code is;

<?
//select the description
$descriptionQuery = "SELECT productDescription.description
FROM products, productDescription
WHERE products.productID = '$productID'
AND productDescription.productID = products.productID";

//store the result set into the the variable $result
$result = mysql_query($descriptionQuery);

while($description = mysql_fetch_array($result))
{
$productDescription = "$description[description]";

$list = str_replace(";", "<li><span class='productDescription'>", $productDescription);

}
?>

any help would be much appriciated :D

NOTE: $list is echoed out on another script incase ure wondering

The Little Guy
09-27-2006, 12:56 PM
<?
//select the description
$descriptionQuery = "SELECT productDescription.description
FROM products, productDescription
WHERE products.productID = '$productID'
AND productDescription.productID = products.productID";

//store the result set into the the variable $result
$result = mysql_query($descriptionQuery);

while($description = mysql_fetch_array($result))
{
$productDescription = "$description[description]";

echo str_replace(";", "<li><span class='productDescription'>", $productDescription).'</span></li>';

}
?>

j-dearden
09-27-2006, 01:01 PM
Ive already tried that it adds it to the end of the entire list, i also tried exploding the string first where there is a ; and then use a foreach loop to echo

<li><span>$list</span></li>

but this placed the paragraph which is not part of a list as a list which osnt what im looking for cheers for the help tho.

any more suggestions?

bokeh
09-27-2006, 01:02 PM
$list = "<li><span class='productDescription'>".str_replace(";", "</span></li>\n<li><span class='productDescription'>", $productDescription).'</span></li>';

j-dearden
09-27-2006, 01:05 PM
there isnt a way of knowing how many list elements there are for each product some may have 2 bullets where another may have 7 wouldnt that only display 2 list items?

The Little Guy
09-27-2006, 01:11 PM
No

Your doing this right?

<?
//select the description
$descriptionQuery = "SELECT productDescription.description
FROM products, productDescription
WHERE products.productID = '$productID'
AND productDescription.productID = products.productID";

//store the result set into the the variable $result
$result = mysql_query($descriptionQuery);

while($description = mysql_fetch_array($result))
{
$productDescription = "$description[description]";

$list = "<li><span class='productDescription'>".str_replace(";", "</span></li>\n<li><span class='productDescription'>", $productDescription).'</span></li>';
echo $list;
}
?>

bokeh
09-27-2006, 01:17 PM
wouldnt that only display 2 list items?It's dynamic but the real problem is the way you are storing the data. You should have a seperate field for each datum rather than using a delimiter. And what if the delimiter appears natively in the text?

j-dearden
09-27-2006, 01:22 PM
what problems would occur if </span> and </li> are missing? because it works fine without them and if there is no major problem its not realy something to stress about like i said it did work as it was i just thought it would be prefereable to keep with standards.

bokeh
09-27-2006, 01:38 PM
What's the point using invalid mark-up when it is so easy to do it right? Maybe it seems to work ok now but if you continue with this ideology you will end up with compounded errors that will have strange side effects in certain browsers (or all browsers).

j-dearden
09-27-2006, 01:50 PM
thats why i dont realy want to leave it as it is, the way u showd me workd but i get the same result as i do when i exploded the string into several array elements where ; is present then for each array element echoed it out as a list item, but it placed the paragraph as a list item when its a paragrapth.

for example say i have a description in my table like;

-----------------------------------------------------------------------

Manufactured in accordance with the exacting standards and requirements featured in BS EN471:1994 High Visibility clothing & BS EN340: 1993.; 3M Scotchlite reflective tape ; Fully taped and waterproof seams ; ENV343 Class 3 waterproof shell fabric ; Front patch pockets ; Full length zip & storm fastening ; Storm cuffs & roll away hood ;Generous sizing

------------------------------------------------------------------------

the first few lines upto the first ; is a paragrapth everything else is a list item
so in the browser it looks like;

----------------------------------------------------------------------

Manufactured in accordance with the exacting standards and requirements featured in BS EN471:1994 High Visibility clothing & BS EN340: 1993.

; 3M Scotchlite reflective tape
; Fully taped and waterproof seams
; ENV343 Class 3 waterproof shell fabric
; Front patch pockets
; Full length zip & storm fastening
; Storm cuffs & roll away hood
; Generous sizing

-----------------------------------------------------------------------

all ; are replaced with a bullet, as u can see i need to start the list at the first ;

thanks again.

The Little Guy
09-27-2006, 01:55 PM
What if your data base, you started a li with ;; and ended the li with ;;;

j-dearden
09-27-2006, 01:58 PM
and then replace all occurences of ;;; with </li>

The Little Guy
09-27-2006, 01:59 PM
yes

or </span></li>

j-dearden
09-27-2006, 02:03 PM
sounds feasable could give that a try

would you have to concatinate the 2 together using . ?

cheers

The Little Guy
09-27-2006, 02:06 PM
$remove = array(" ;; "," ;;; ");
$replace = array("<li><span class=\"productDescription\">", "</span></li>");
$list = str_replace($remove, $replace, $productDescription);
echo $list;

j-dearden
09-27-2006, 02:09 PM
Cheers for that m8 will give it a try. its actualy a simple concept well u learn something new every day thanks again m8 :D

bokeh
09-27-2006, 02:30 PM
$list = preg_replace('/;([^;]*)/', "<li><span class='productDescription'>$1</span></li>\n", $productDescription); What if your data base, you started a li with ;; and ended the li with ;;;Why go from bad to worse? If you were going to that trouble you might as well just put the tags in the DB. A better way tough would be one field per <li>.

j-dearden
09-27-2006, 03:32 PM
the problem with having a field per list is that as i said before each product has a different number of lists items, if i have a field for each list item il have records with empty fields and thats jus a waste of space.

bokeh
09-27-2006, 04:02 PM
Have you tried that code yet?