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 4 of 4
  1. #1
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts

    Placing tags in string on position

    Actually i have an array of starting positions like this

    EDIT: Array ( [0] => 2 ) Array ( [0] => 5 ) Array ( [0] => 8 )

    and after 2 characters, all of the chars need to be bolded

    How can i place html tags eg <b></b> to string based on that array

    PHP Code:
    $string "Textthatisnotbold";

    $positions = array( 258);
    $positionend 2;

    function 
    place_tag($string$positions$positioned) {

    //functiont to give THIS result


    Code:
    Te<b>xt</b>t<b>ha</b>tis<b>not</b>bold
    Crack this one guyz, and you got beer from me
    Last edited by matak; 06-01-2007 at 06:48 PM. Reason: This is what i really need :)

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Try this one out -
    PHP Code:
    <?php
    function place_tags($string,$positions,$positionend)
    {
        
    $count count($positions);
        
    $nextchar 0;
        for(
    $i 0;$i<$count;$i++)
         {
            
    $newstring .= substr($string,$nextchar,$positions[$i]-$nextchar);
            
    $newstring .= "<strong>"substr($string,$positions[$i],$positionend) ."</strong>";
            
    $nextchar $positions[$i]+$positionend;
         }
         
    $newstring .= substr($string,$nextchar);
         return 
    $newstring;
    }
    $string "Textthatisnotbold";
    $positions = array(2,5,10);
    $positionend 2;
    $formatted_string place_tags($string,$positions,$positionend);
    print 
    $formatted_string;
    ?>
    HTH,
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    New Coder
    Join Date
    Sep 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Without putting to much thought into it...

    PHP Code:
    <?php

    function add_tags $str$len$pos$tag )
    {
        
    $str str_split $str );

        
    $len -= 1;

        foreach ( 
    $pos AS $v )
        {
            
    $str[$v] = $tag[0] . $str[$v];
            
    $str[$v+$len] .= $tag[1];
        }

        return 
    join ''$str );
    }

    $len 2;

    $str "Textthatisnotbold";

    $pos = array ( 25);

    $tag = array ( '<b>''</b>' );

    echo 
    add_tags $str$len$pos$tag );

    ?>

  • #4
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Thank you guys very much, both of those functions show me a great path to solving my problem, as they are simple and work in order. I'll continue to study them a bit more tomorow.

    You got your selfz a beer (or more) from me


  •  

    Posting Permissions

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