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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts

    Search for first instance

    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:
    PHP Code:
    $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.
    Last edited by treeleaf20; 06-28-2010 at 09:46 PM.

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    First off, let's use arrays:
    PHP Code:
    $replaceThese = array(
    "<endbullet><bullet>"
    ,"<bold>"
    ,"<endbold>"
    ,"<endbullet>"
    "<bullet>"
    );
    $withThese = array(
    "</li><li>"
    ,"<strong>"
    "</strong>"
    "</li><br>"
    ,"<li>"
    );

    str_replace($replaceThese$withThese$newTxt); 
    Much nicer. Ok, now, you need to find the first instance of <li> in new text, break the string at that spot, and add the <ul>. You do that with strpos, 2 calls to substr (get the string before the <li> and the string including and after the <li>) and a concatenation ($firstPart."<ul>".$secondPart). Then you repeat this process for the </ul> using strrpos, which searches backwards for the first instance of a string.

    Good luck!
    Last edited by Beagle; 06-29-2010 at 08:53 AM. Reason: missing comma


  •  

    Posting Permissions

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