SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Inserting Stuff in Database Text

    Suppose I have the following text stored inside a cell in my database table...

    Code:
    <p class="pfirst">This is the first paragraph.</p>
    
    <p>But not the last one.</p>
    
    <p>NOT the end...</p>
    Then suppose I display this on a page with an echo variable - $Text.

    In fact, I have lots of database articles that look something like this. The first paragraph is usually, but not always, styled class="pfirst".

    Is there a way to insert something into my text using PHP or regex? For example, suppose I decide to insert an image before the second paragraph in every article, or maybe I want to display a table before every third paragraph.

    I would probably wind up building a switch that inserts the object before every second paragraph, EXCEPT for a few special cases where it would come before the third paragraph or perhaps not be inserted at all.

    Is there a way to do this?

    Thanks.

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In fact, I have lots of database articles that look something like this. The first paragraph is usually, but not always, styled class="pfirst".
    First of all how do you assign dynamically that class pfirst in the first paragraph that is not always but sometime only?

    Is there a way to insert something into my text using PHP or regex? For example, suppose I decide to insert an image before the second paragraph in every article,
    If your image is always fixed (i mean particular one like logo) and you are sure that there will always be the opening and closing <p></p> tags in the article i think you can have some regex to find the first closing tag and insert your image just after it.

    maybe I want to display a table before every third paragraph
    I am not sure about this like what you mean.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2007
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $imgSrc = '/images/random.jpeg';

    echo preg_replace('#^\s+(\<p class="pfirst"\>)#i', "$1<img src="$imgSrc" style="float: left" />", $body);

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Speeple View Post
    $imgSrc = '/images/random.jpeg';

    echo preg_replace('#^\s+(\<p class="pfirst"\>)#i', "$1<img src="$imgSrc" style="float: left" />", $body);
    Thanks, but I'm having trouble implementing that. I changed your script just a bit to escape some syntax errors, then I inserted it like this:

    PHP Code:
    echo preg_replace('#^\p class="pfirst"\>)#i'"$1<img src=\"/images/logos/IVR.gif\" style=\"float: right\" />"$body);

    echo 
    $text;

    ...where $text displays an article from my database. I also tried it like this:

    PHP Code:
    echo $text;

    echo 
    preg_replace('#^\p class="pfirst"\>)#i'"$1<img src=\"/images/logos/IVR.gif\" style=\"float: right\" />"$body);

    Then, I placed your script outside the while loop, with the same results.

    Thanks.

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just realized $body should be $text (I think), but after changing it, all it does is echo the database article; it doesn't replace anything.


Bookmarks

Posting Permissions

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