Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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