SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    May 2002
    Location
    Oslo
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Posting a sample would have deffinatly helped in explaning exactly what you are trying to do.

    I interpreted it as having a string like "blah something blah" and then you want to have PHP search the string for "something" and if it finds it, add something to the string (like " else", for example) after where "something" was found.

    If that is the case, you can do it like this:

    PHP Code:
    $text 'blah something blah';
    echo 
    str_replace('something''something else'$text); 
    That would echo "blah something else blah". You can change the first two arguments to str_replace() as needed. Is that what you want?

  3. #3
    SitePoint Zealot
    Join Date
    May 2002
    Location
    Oslo
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok heres 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;
    }

  4. #4
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. The code I posted should do almost exactly that.

    Use:

    PHP Code:
    $text str_replace('something''something else'$text); 
    Let me know if you need any more help.

  5. #5
    SitePoint Zealot
    Join Date
    May 2002
    Location
    Oslo
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Only problem is that in your code is that the expression you search for get replaced. Say that the expression is 1000 characters long, it would'nt be very efficient. I need to insert 'else' , and only that, behind the expression searhed for.

  6. #6
    SitePoint Zealot
    Join Date
    May 2002
    Location
    Oslo
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  7. #7
    SitePoint Zealot
    Join Date
    May 2002
    Location
    Oslo
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this:

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

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

  8. #8
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this instead:
    Code:
    <?php
    $search = 'test';
    
    $string = "this is a test. we'll have another test tommorrow.";
    
    $add = ' really!';
    
    $string = preg_replace("/(.+)$search/U", "$1 $search"."$add", $string, 1);
    
    echo $string;
    ?>
    your regex must be properly delimited by non-alphanumeric characters (i used backslashes above)

  9. #9
    SitePoint Zealot
    Join Date
    May 2002
    Location
    Oslo
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanx!!! it works perfectly
    I dont understand anything of the code, but as long as it works i'm happy

  10. #10
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $string = preg_replace("/(.+)$search/U", "$1 $search"."$add", $string, 1);
    no problem

    that's the crux up above... the 1st parameter is the regex which is delimited by the slashes '/'... the parentheses capture the parts of the string that match '.+', which basically means '1 or more of any character'... we want to follow that up with $search so that we know where we can add the string after... the 'U' behind the modifiers makes the regex 'ungreedy' so that it doesn't eat up $search terms in the string if there are more than 1 occurence (since we only want to replace the one after the 1st occurence of $search)...


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
  •