Web Analytics Made Easy -
StatCounter Insert a string into a string - CodingForum

Announcement

Collapse
No announcement yet.

Insert a string into a string

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

  • Insert a string into a string

    I have a string which I want to search through for a given expression. If I find this expression I want to insert a new string behind it. How do I do this?

  • #2
    To clarify things, here's what I have in mind:

    $string = "blah something blah";
    $expression = "something";
    $insertvalue = "else";

    search through $string for $expression;

    if($string contains $expression)
    {
    insert $insertvalue into $string ->behind<- $expression;
    }

    Comment


    • #3
      well

      PHP Code:
      <?
      $string 
      "wombats are harmless"
      $expression "harmless"
      $insertvalue " (mostly)"

      if(
      strstr($string,$expression)){
          
      $string=str_replace($expression,$expression.$insertvalue,$string);
      }

      echo 
      $string;
      ?>
      but so does

      PHP Code:
      <?
      echo  str_replace($expression,$expression.$insertvalue,$string);
      ?>
      not sure if there is a reason you want to actually scan to see if the string exists in the firstplace?
      resistance is...

      MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

      Comment


      • #4
        str_replace

        ok I've altered my code a bit so it works with str_replace. Only problem now is that str_replace replaces all occurrences of the search string with the replacement string. Is there a way to get it to only replace the first occurence of the search string?

        Comment


        • #5
          I tried this:

          preg_replace($expression,$expression.$insertvalue,$string,1);

          always get errormessage saying that delimiter must be alphanumeric???????????

          Comment


          • #6
            well using regex should be a last resort... but this works..

            PHP Code:
            <?
            $string 
            "wombats are harmless but not as harmless as llamas"
            $expression "harmless"
            $insertvalue " (mostly)"

            $string=preg_replace("|$expression|",$expression.$insertvalue,$string,1); 
            echo 
            $string;
            ?>
            resistance is...

            MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

            Comment


            • #7
              What you could do is...

              PHP Code:
              <? 
              //-----------------------------------------------
              $string "wombats are harmless"
              $expression "harmless"
              $insertvalue " (mostly)"

              $pieces explode($expression,$string);
              $mod_string $pieces[0] . $expression $insertvalue $pieces [1];

              $n count($pieces);
              for(
              $i 2;$i $n;$i++)
              {
                
              $mod_string .= $expression $pieces[$i]
              }
              //-----------------------------------------------
              ?>
              ...should work... havn't tried though.
              I don't suffer from insanity, I enjoy every single minute of it!

              Comment

              Working...
              X