sradha
12-10-2010, 10:04 AM
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"
|
||||
replace only the first occurence of a charactersradha 12-10-2010, 10:04 AM 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" firepages 12-10-2010, 10:24 AM this works ... but I am positive there is an easier/better way ...but it escapes me ATM $str='s?d1fg&sdsdf&fg&'; $str{strpos($str, '&')}='?'; echo $str; sradha 12-10-2010, 10:55 AM 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. firepages 12-10-2010, 11:09 AM Hi, you can access part of a string (which in PHP is scalar) as if it were an array, so ... $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 ? abduraooft 12-10-2010, 11:11 AM $str='s?d1fg&sdsdf&fg&'; $str{strpos($str, '&')}='?'; echo $str; is equivalent to $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; [] OR {} - http://www.codingforums.com/showthread.php?t=93902 :) sradha 12-10-2010, 11:18 AM 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? firepages 12-10-2010, 11:24 AM ok, from the php strings page (http://php.net/manual/en/language.types.string.php) 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 ?? sradha 12-10-2010, 11:25 AM // I'm seeing this curly brace syntax for the first time! [] OR {} - http://www.codingforums.com/showthread.php?t=93902 :) 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. |
| |||
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum