Jump to content

Help with String Replacing


treeleaf20

Recommended Posts

All,

I have the following text:

<bold>Our Services Include:<endbold>

 

<bullet>Construction<endbullet><bullet>Landscaping<endbullet>

 

Then I have the following PHP to replace the contents:

$qrytxt = "Select * from texts where text_id='2'";
$resulttxt = mysql_query($qrytxt);
$resultsettxt = mysql_fetch_array($resulttxt);
$newtxt = $resultsettxt['text'];
$newtxt = str_replace("<endbullet><bullet>", "</li><li>", $newtxt);
$newtxt = str_replace("<bullet>", "<li>", $newtxt);
$newtxt = str_replace("<endbullet>", "</li><br>", $newtxt);
$newtxt = str_replace("<bold>", "<strong>", $newtxt);
$newtxt = str_replace("<endbold>", "</strong>", $newtxt);
echo nl2br($newtxt);  

My problem is that if it is the first <bullet> then I want it to replace to <ul><li> instead of just a <li> and also if it's the last <endbullet> then I want it to replace it with </li></ul>

 

Any ideas on how to do that?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/206122-help-with-string-replacing/
Share on other sites

Perhaps try...

 

$newtxt = "<UL><br>" . $resultsettxt['text'];
$newtxt = str_replace("<endbullet>", "</li><br>", $newtxt);
$newtxt = str_replace("<bullet>", "<li>", $newtxt);
$newtxt = str_replace("<bold>", "<strong>", $newtxt);
$newtxt = str_replace("<endbold>", "</strong>", $newtxt);
$newtxt = $newtxt . "</ul><br>";
echo $newtxt;  

try

<?php
$test = '<bold>Our Services Include:<endbold>

<bullet>Construction<endbullet><bullet>Landscaping<endbullet>';
$test = preg_replace('/<bold>(.*?)<endbold>/s', '<strong>$1</strong>', $test); 
$test = preg_replace('/<bullet>(.*?)<endbullet>/s', '<li>$1</li>', $test);
$test = preg_replace('/<li>.*<\/li>/s', '<ul>$0<br /></ul>', $test);
echo $test;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.