Goosio Posted February 5, 2010 Share Posted February 5, 2010 I'm trying to replace only the first instance of a substring within a string. In this case, I am trying to replace the first instance of "two" with "2" and the second instance of "two" with 16. (Actually here I'm changing both instances to 16, and then the first one to 2.) Preg_replace here appears to give me a null string....at any rate there doesn't appear to be anything in the string. $string="one two three four five four three two one"; $search="two"; $firstreplace=2; $otherreplace=16; $string=str_replace($search,$otherreplace,$string); $string=preg_replace($otherreplace,$firstreplace,$string,1); I'm sure I'm missing something really simple here. Any ideas? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/190998-preg_replace-newbie-question/ Share on other sites More sharing options...
MadTechie Posted February 5, 2010 Share Posted February 5, 2010 <?php $string="one two three four five four three two one"; $search="two"; $firstreplace=2; $otherreplace=16; $string=str_replace($search,$otherreplace,$string); $string=preg_replace("/$otherreplace/i",$firstreplace,$string,1); echo $string; one 2 three four five four three 16 one However this is neater/easier to read <?php $string="one two three four five four three two one"; $search="two"; $firstreplace=2; $otherreplace=16; $string=preg_replace("/$search/i",$firstreplace,$string,1); $string=preg_replace("/$search/i",$otherreplace,$string,1); echo $string; Quote Link to comment https://forums.phpfreaks.com/topic/190998-preg_replace-newbie-question/#findComment-1007155 Share on other sites More sharing options...
Goosio Posted February 5, 2010 Author Share Posted February 5, 2010 Hi, thanks for that! It works! But I still can't figure out why and I'm hoping you can explain to me. Obviously I need the "/..../i" As near as I can tell is the /i makes the search case insensitive...but why would that matter? The $search and $string are both lowercase. What's even weirder to me is why I would need the quotemarks....why doesn't $search become a quote? Why doesn't it lose its variable properties? Sorry if this is all painfully obvious. As I said I'm just starting out. Appreciate your help. Quote Link to comment https://forums.phpfreaks.com/topic/190998-preg_replace-newbie-question/#findComment-1007538 Share on other sites More sharing options...
MadTechie Posted February 5, 2010 Share Posted February 5, 2010 Basically you need the start and end delimiters, in my example I used / now in preg you MUST the first character becomes the delimiter, and the RegEx ends when it meets another one, any thing after the end delimiter must be a switch, in my example I used i which means case insensitive. as for the quotes, anything inside double quotes is parsed by PHP for example (pay attention to the text formatting / colour) blue being a variable and red a string, your notice one of the $test is red, thus its a string! $test = "testing"; echo $test; //this will return -> testing //also echo "$test"; //this will return -> testing //and echo "$test 123"; //this will return -> testing 123 //however echo '$test 123'; //this will return -> $test 123 //you could also concatenate (using a . ) echo $test.' 123'; //this will return -> testing 123 //So how to quote ? //Well you could either escape or quote //single quotes echo $test.' "123" '; //this will return -> testing "123" //escape echo "$test \"123\" "; //I added a space to make it clearer : returns -> testing "123" Now with all that info I could of created the RegEx like this $string=preg_replace("/".$otherreplace."/i",$firstreplace,$string,1); Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/190998-preg_replace-newbie-question/#findComment-1007556 Share on other sites More sharing options...
Goosio Posted February 5, 2010 Author Share Posted February 5, 2010 I see, thanks for the detailed explanation! So I guess in my original code: $string=preg_replace($otherreplace,$firstreplace,$string,1); The $ in $otherreplace was taken as a delimiter, which basically loused up the entire syntax. Please let me know if I'm misunderstanding that! Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/190998-preg_replace-newbie-question/#findComment-1007560 Share on other sites More sharing options...
MadTechie Posted February 5, 2010 Share Posted February 5, 2010 Not exactly, $otherreplace is a variable so $otherreplace="16"; $string=preg_replace($otherreplace,$firstreplace,$string,1); is was exactly the same as it you typed $string=preg_replace("16",$firstreplace,$string,1); a variable is a keyword or phrase that is linked to a value stored in the system's memory or an expression that can be evaluated. For instance, a variable might be called "total_count" and contain a number. ie if $tax = 1.75; then 10 + $tax = 11.75 in other words its evaluating to this 10 + 1.75 = 11.75 make sense ? Quote Link to comment https://forums.phpfreaks.com/topic/190998-preg_replace-newbie-question/#findComment-1007621 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.