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 8 of 8
  1. #1
    New Coder
    Join Date
    Feb 2010
    Location
    God's Own Country
    Posts
    87
    Thanks
    6
    Thanked 0 Times in 0 Posts

    replace only the first occurence of a character

    If I have a string like "abcd&123&efgh&456", what function in PHP should I use to replace only the first occurence of '&' with some other character say '?'. The resultant string I need is "abcd123&efgh&456"

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    this works ... but I am positive there is an easier/better way ...but it escapes me ATM
    PHP Code:
    $str='s?d1fg&sdsdf&fg&';
    $str{strpos($str'&')}='?';
    echo 
    $str
    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)

  • #3
    New Coder
    Join Date
    Feb 2010
    Location
    God's Own Country
    Posts
    87
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks my friend..ofcourse it works..but how does it work exactly. strpos() finds the position of first "&" and then it gets replaced with "?". I didn't get the logic of what is happening after getting the postion of first occurence of '&' character.

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    Hi, you can access part of a string (which in PHP is scalar) as if it were an array, so ...
    PHP Code:
    $str='blah';
    echo 
    $str{1}; //echo's 'l'
    #or
    echo $str[0]; //echo's 'b' 
    so we simply act on the string as if it were an array and change $str[$index] to something else, this does not however work if you want to remove a portion of the string.. $str[$index]=''; , does not actually work (at least not for me on linux)

    I cant remember the name of this way of accessing a string and I think the {} method might be depracated ... or is it the [] method ?
    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)

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,851
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    $str='s?d1fg&sdsdf&fg&';
    $str{strpos($str'&')}='?';
    echo 
    $str
    is equivalent to
    PHP Code:
    $str='s?d1fg&sdsdf&fg&';
    $pos=strpos($str'&');
    $str{$pos}='?'#=> $str[$pos]='?';  // I'm seeing this curly brace syntax for the first time!
    echo $str
    Last edited by abduraooft; 12-10-2010 at 11:15 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    New Coder
    Join Date
    Feb 2010
    Location
    God's Own Country
    Posts
    87
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages View Post
    I cant remember the name of this way of accessing a string and I think the {} method might be depracated ... or is it the [] method ?
    ok..anyways both method works fine.
    So should I use {} or []..if it's treated as an array the I think we should go for []..what do u feel?

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    ok, from the php strings page

    Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose. The functions substr() and substr_replace() can be used when you want to extract or replace more than 1 character.

    Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose.
    so looks like either is good, whichever you prefer, I think curly braces as then you are less likely to confuse it with an array ??
    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)

  • #8
    New Coder
    Join Date
    Feb 2010
    Location
    God's Own Country
    Posts
    87
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    PHP Code:
     // I'm seeing this curly brace syntax for the first time! 
    I am also seeing the use of {} like this now..Is it a safe method? Are there any other alternative methods for finding and replacing first occurence of a string.


  •  

    Posting Permissions

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