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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 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
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
    }

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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)

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried this:

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

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

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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)

  • #7
    New Coder
    Join Date
    Jun 2002
    Location
    Paris, France
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

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