www.webdeveloper.com
Results 1 to 7 of 7

Thread: adding html code with preg_replace()

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Posts
    13

    adding html code with preg_replace()

    I'm using a preg_replace() search to place html <h3> tags into code before it's sent to a mySQL database. The following code works fine -- it finds lines without an opening h3 tag and inserts opening and closing h3 tags:

    Code:
    if ((substr ($cnt, 1, 3) != "h3>")){
    		$cnt="<h3>" . preg_replace("/(\n|\r\n)+/", "</h3>\n\n<h3>", $cnt) . "</h3>";
    	}
    THE PROBLEM: I also want to avoid adding h3 tags to list -- <ul>, <li> -- elements. How do I do that? This doesn't work:

    Code:
    if ((substr ($cnt, 1, 3) != "h3>") && (substr ($cnt, 1, 3) != "ul>") && (substr ($cnt, 1, 3) != "li>") ){
    		$cnt="<h3>" . preg_replace("/(\n|\r\n)+/", "</h3>\n\n<h3>", $cnt) . "</h3>";
    	}

  2. #2
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Can you please provide an example of the input text and desired output?

  3. #3
    Join Date
    Jan 2006
    Posts
    13
    Sure, thanks.

    Step 1: Here's an example of some original text:
    ---
    HTML Code:
    Here's some text. Any parapraph of plain text. 
    
    <ul>
    <li>first list item;</li>
    <li>second item in the list;</li>
    <li>another list item.</li>
    </li>
    
    And more paragraphs of plain text like this.
    ---

    Step 2: Here's what I want after running preg_replace() - h3 tags are added to the untagged paragraphs but not to lines that have <ul> and <li> tags:
    ---
    HTML Code:
    <h3>Here's some text. Any parapraph of plain text.</h3>
    
    <ul>
    <li>first list item;</li>
    <li>second item in the list;</li>
    <li>another list item.</li>
    </ul>
    
    <h3>And more paragraphs of plain text like this.</h3>
    ---

    Thanks for any help you can offer.

  4. #4
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    PHP Code:
    <?php
    ob_start
    ();
    ?>
    Here's some text. Any parapraph of plain text. 

    <ul>
    <li>first list item;</li>
    <li>second item in the list;</li>
    <li>another list item.</li>
    </li>

    And more paragraphs of plain text like this.
    <?php
    $text 
    ob_get_clean();

    echo 
    preg_replace("/^([^<]+)$/mi""<h3>$1</h3>"$text);
    ?>

  5. #5
    Join Date
    Jan 2006
    Posts
    13
    Thanks for your work, but I'm not sure how to incorporate it into my existing function. And since I know little about regular expressions or the output buffer, I'm going to have to spend more time on it, and more likely get rid of the <ul> code altogether.

  6. #6
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Quote Originally Posted by trick_of_light
    Thanks for your work, but I'm not sure how to incorporate it into my existing function. And since I know little about regular expressions or the output buffer, I'm going to have to spend more time on it, and more likely get rid of the <ul> code altogether.
    Here is a version without using the output buffer and I have wrapped it in to a function for you.

    PHP Code:
    <?php
    function add_html_tags($text$tag="h3")
    {
        return 
    preg_replace("/^([^<]+)$/mi""<".$tag.">$1</".$tag.">"$text);
    }


    $text "Here's some text. Any parapraph of plain text.

    <ul>
    <li>first list item;</li>
    <li>second item in the list;</li>
    <li>another list item.</li>
    </li>

    And more paragraphs of plain text like this."
    ;

    echo 
    add_html_tags($text);
    ?>
    Last edited by MrCoder; 06-01-2007 at 03:44 AM.

  7. #7
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062
    try...
    PHP Code:
    <?php

    $text 
    "Here's some text. Any paragraph of plain text.

    <ul>
    <li>first list item;</li>
    <li>second item in the list;</li>
    <li>another list item.</li>
    </li>

    And more paragraphs of plain text like this."
    ;

    $line  explode("\n"$text);
    foreach(
    $line as $l) {

        
    $start substr($l03);
        if(!
    preg_match("/</i"$start)){
            
    $new .= preg_replace("/^([^<]+)$/mi""<h3>$1</h3>"$l);
        }else{
            
    $new .= $l;
        }

    }

    echo(
    $new);
    ?>
    Last edited by Sheldon; 05-31-2007 at 08:08 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles