SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding html tags to a string programmatically

    Hello Sitepoint'ers,


    I need to put together a couple of functions that do the following:-

    The first takes any given string and adds and opening span tag to the front, and a closing tag to the end.

    The second takes another string, and adds span tags to the very first letter in the string.

    If someone could point me in the right direction I would be most apprecative!



    Kind regards,


  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Well #1 is just string concatenation. ( which is . in php)
    #2 is also string concatenation, but with a bit of substr thrown in.

  3. #3
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI, thanks for your reply. I forget to mention that I only want these tags added if they dont already exist as part of the string

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Ah. Now we're getting a bit more sensitive...

    "if they dont already exist as part of the string"... does that mean ANYWHERE in the string? or specifically at the beginning and ends?

  5. #5
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Scrap that, specifically where I want them to be added originally. Its kinda like a saftey net that if my users dont add them, they will be added automatically before submitting to the database (I hope that makes sense)

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Yup.

    Well, since we're working with just one tag, wont be that hard.
    substr the left and right sides of the string (Hint: the Start parameter can take a negative integer.), and see if they are a span tag. If not, slap the tag on.

  7. #7
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok Sir, I will try that thanks

  8. #8
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    This will do what you're asking.

    Code php:
    $string = 'This is a string.';
    $first_letter = substr($string, 0, 1);
    $remaining_string = substr($string, 1);
     
    if(substr($string, 0, 5) != '<span') {
     $string = '<span><span>'.$first_letter.'</span>'.$remaining_string;
    }
    if(substr($string, -7) != '</span>') {
     $string .= '</span>';
    }

    If you're trying to style the first letter, however, you'd be better off using the CSS pseudo-class :first-letter:

    Code php:
    $string = 'This is a string.';
     
    if(substr($string, 0, 5) != '<span') {
     $string = '<span class="some_class">'.$string;
    }
    if(substr($string, -7) != '</span>') {
     $string .= '</span>';
    }

    Code css:
    span.some_class:first-letter {
    /* some styling */
    }
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  9. #9
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brilliant post, ill try that in the morning thanks again!


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
  •