HoTDaWg Posted August 18, 2009 Share Posted August 18, 2009 hi there i get a feeling this is a really stupid question and i apologize for not trying harder to figure it out, i just cant get my head around it and my deadline is slowly winding down. im trying to replace a string such as "8 2009" (or "12 2009") to just "8" here is what i have been experimenting with, but i cant seem to get it right: <?php $thatmonth = "8 2009"; preg_replace('/^[\d]+[\s\d]+$/','/^[\d]+$/',$thatmonth) ?> any help would be greatly appreciated, thanks HoTDaWg Quote Link to comment https://forums.phpfreaks.com/topic/170776-solved-changing-8-2009-to-just-8/ Share on other sites More sharing options...
Garethp Posted August 18, 2009 Share Posted August 18, 2009 preg_replace('~([0-9]+) 2009~', '$1', $Content); Quote Link to comment https://forums.phpfreaks.com/topic/170776-solved-changing-8-2009-to-just-8/#findComment-900657 Share on other sites More sharing options...
trq Posted August 18, 2009 Share Posted August 18, 2009 If that is your entire string you can simply use string indexes. $thatmonth = "8 2009"; echo $thatmonth[0]; Quote Link to comment https://forums.phpfreaks.com/topic/170776-solved-changing-8-2009-to-just-8/#findComment-900662 Share on other sites More sharing options...
HoTDaWg Posted August 18, 2009 Author Share Posted August 18, 2009 awesome thanks! Quote Link to comment https://forums.phpfreaks.com/topic/170776-solved-changing-8-2009-to-just-8/#findComment-900674 Share on other sites More sharing options...
nrg_alpha Posted August 18, 2009 Share Posted August 18, 2009 If that is your entire string you can simply use string indexes. $thatmonth = "8 2009"; echo $thatmonth[0]; That would work on single digit months... but in the event of double digits, you would have to know that there are two digits and echo out the first two indexes... based on the same assumptions (that the string only consists the format of month[space]year), sscanf comes to mind on this one for me: $thatmonth = "12 2009"; sscanf($thatmonth, '%d', $month); echo $month; // 12 Quote Link to comment https://forums.phpfreaks.com/topic/170776-solved-changing-8-2009-to-just-8/#findComment-901018 Share on other sites More sharing options...
Garethp Posted August 19, 2009 Share Posted August 19, 2009 What are string indexes? I've worked with them in Python, but I didn't know they existed in PHP Quote Link to comment https://forums.phpfreaks.com/topic/170776-solved-changing-8-2009-to-just-8/#findComment-901598 Share on other sites More sharing options...
nrg_alpha Posted August 19, 2009 Share Posted August 19, 2009 What are string indexes? I've worked with them in Python, but I didn't know they existed in PHP Well, a string is a series of charactes of which are stored in indexes.. Example: $str = 'cat'; echo $str[0] . "<br />\n"; // output: c echo $str[1] . "<br />\n"; // output: a echo $str[2] . "<br />\n"; // output: t Quote Link to comment https://forums.phpfreaks.com/topic/170776-solved-changing-8-2009-to-just-8/#findComment-901605 Share on other sites More sharing options...
Garethp Posted August 19, 2009 Share Posted August 19, 2009 Can you do $str[0,2] like in Python? Quote Link to comment https://forums.phpfreaks.com/topic/170776-solved-changing-8-2009-to-just-8/#findComment-901607 Share on other sites More sharing options...
nrg_alpha Posted August 19, 2009 Share Posted August 19, 2009 I'm not versed in python, so I don't know what $str[0,2] does.. is that like a range from index 0 to 2 or something? With regards to php, I don't think you can specify something like that. Quote Link to comment https://forums.phpfreaks.com/topic/170776-solved-changing-8-2009-to-just-8/#findComment-901617 Share on other sites More sharing options...
trq Posted August 19, 2009 Share Posted August 19, 2009 Can you do $str[0,2] like in Python? Nope. You would need to use substr. Quote Link to comment https://forums.phpfreaks.com/topic/170776-solved-changing-8-2009-to-just-8/#findComment-901625 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.