kks_krishna Posted November 13, 2007 Share Posted November 13, 2007 HI, I have the following string "12-what-is-clonable-interface-in-java/". In that I want to split "12" and "what-is-clonable-interface-in-java" as two parts. Can anyone help me to to do this. Link to comment https://forums.phpfreaks.com/topic/77141-help-needed-on-substring/ Share on other sites More sharing options...
Aureole Posted November 13, 2007 Share Posted November 13, 2007 Is the first part you want to split always going to be 2 characters long? If so then I think I know how. Link to comment https://forums.phpfreaks.com/topic/77141-help-needed-on-substring/#findComment-390607 Share on other sites More sharing options...
kks_krishna Posted November 13, 2007 Author Share Posted November 13, 2007 NO..It can be anything but number. It may be 12 or 8 or 999 etc.. Link to comment https://forums.phpfreaks.com/topic/77141-help-needed-on-substring/#findComment-390608 Share on other sites More sharing options...
Aureole Posted November 13, 2007 Share Posted November 13, 2007 Oh ok I get you, I really have no idea sorry. Someone will... give it time. Link to comment https://forums.phpfreaks.com/topic/77141-help-needed-on-substring/#findComment-390614 Share on other sites More sharing options...
kks_krishna Posted November 13, 2007 Author Share Posted November 13, 2007 HI, I have writen the code and its working fine. Please anyone check for the correctness of the code. If you know any better approach please guide me. The following code i have written: $q_url_id = $_GET['q_url_id']; $chars = preg_split('[-]', $q_url_id, 2, PREG_SPLIT_NO_EMPTY); print_r($chars); Link to comment https://forums.phpfreaks.com/topic/77141-help-needed-on-substring/#findComment-390628 Share on other sites More sharing options...
sasa Posted November 13, 2007 Share Posted November 13, 2007 try <?php $a = '12-what-is-clonable-interface-in-java/'; preg_match('/^([0-9]+)-(.+)$/',$a,$b); echo $b[1], "\n<br />\n", $b[2]; ?> Link to comment https://forums.phpfreaks.com/topic/77141-help-needed-on-substring/#findComment-390694 Share on other sites More sharing options...
kenrbnsn Posted November 13, 2007 Share Posted November 13, 2007 You can use the explode() funtion: <?php $str = "12-what-is-clonable-interface-in-java"; list ($first_part, $second_part) = explode('-',$str,2); echo $first_part . ' --- ' . $second_part; ?> Ken Link to comment https://forums.phpfreaks.com/topic/77141-help-needed-on-substring/#findComment-390704 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.