Web Analytics Made Easy -
StatCounter add characters at certain spot - CodingForum

Announcement

Collapse
No announcement yet.

add characters at certain spot

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • add characters at certain spot

    Hi I want to add some characters at a given spot within a string.
    Lets say my string is:

    $image="images/fruit.jpg";

    And I want to add: -2 just before the dot, so it becomes:
    $image="images/fruit-2.jpg";

    Which command or line do I need for this?
    Thanks

  • #2
    if you know exactly where you want to put your string ("-2") then i would use substr_replace

    if it isn't and you want it in a different character place then use preg_match
    :. -= | Eran | =- .:
    __________________

    Comment


    • #3
      Will it always be right before the dot?
      And what you're adding changes each time?

      Comment


      • #4
        Yes it will allways be right before the dot, and -2 will be added (no other characters or numbers)

        Comment


        • #5
          You may want to also consider the use strlen as well.

          Is it possibly to have a file/image name that also a dot in it? ie $image="images/my.fruit.jpg";

          Perhaps look to count the file name length, and then and a -2 from 4th character from the end?

          Example:
          PHP Code:
          $image="images/my.fruit.jpg";

          echo 
          $image .'<br/>';

          $length $image;
          echo 
          strlen($length) .'<br/>'//19

          $location $length 4;

          echo 
          substr_replace($image'-2'$location0) . "<br />\n"
          Last edited by ptmuldoon; Apr 9, 2009, 02:26 PM.

          Comment


          • #6
            ptmuldoon:
            you don't need the strlen function since you can use - sign.
            that mean it counts from the end backwards or in your sample '-4'

            Example:
            PHP Code:
            $image="images/my.fruit.jpg";

            echo 
            $image .'<br/>';

            $length $image;
            echo 
            strlen($length) .'<br/>'//19

            $location $length 4;

            echo 
            substr_replace($image'-2'$location0) . "<br />\n"

            echo 
            "we don't need strlen: " substr_replace($image'-2', -40) . "<br />\n"

            // the result for both is "images/my.fruit-2.jpg" 
            :. -= | Eran | =- .:
            __________________

            Comment


            • #7
              Thanks Never crossed my mind to do it that way.

              Comment


              • #8
                no problem :]
                :. -= | Eran | =- .:
                __________________

                Comment

                Working...
                X