2tonejoe Posted August 4, 2008 Share Posted August 4, 2008 i have strings that come in like: johns-account_p8 johns-account_v2_p3 sams-account sams-account_v2 sams-account_p4 I need a way to check each string, if it has _p in it take the number after the p and turn it into a integer. Once I have this new integer, i need to make a new string variable for every number in the p number (something like these): $newVar1 = johns-account_p1 $newVar2 = johns-account_p2 $newVar3 = johns-account_p3 $newVar4 = johns-account_p4 $newVar5 = johns-account_p5 $newVar6 = johns-account_p6 $newVar7 = johns-account_p7 $newVar8 = johns-account_p8 and if there is no _p then do nothing . . . anyone help out here? Quote Link to comment https://forums.phpfreaks.com/topic/118085-reading-in-string-an-extracting-item/ Share on other sites More sharing options...
Wuhtzu Posted August 4, 2008 Share Posted August 4, 2008 Use some regex. This checks $string to see if it contains any _p followed by a number and if it does it shows you the captured number. <?php $string = 'johns-account_p4'; if(preg_match('/_p([0-9]{1,})/i',$string,$matches)) { print_r($matches); } else { echo 'Does not contain _p followed by a number'; } ?> This should help you get started, but I can't write anything complete because I'm not sure exactly how you store your strings, what you want to do with the captured number ect... The strings hold two names (john and sam) but the new strings only hold one name (john)... Quote Link to comment https://forums.phpfreaks.com/topic/118085-reading-in-string-an-extracting-item/#findComment-607522 Share on other sites More sharing options...
Johntron Posted August 4, 2008 Share Posted August 4, 2008 Try some variation of: preg_match('/_p(\d+)/', $string, $matches); (this will store all of the digits at the end of $string to $matches[1]) If you want each number in a different variable, you could use something like a foreach loop with a preg_match( '/^\d/', $string, $matches). This would match a single digit from the beginning. Inside the foreach-loop, you could save this digit and remove it from $string. Quote Link to comment https://forums.phpfreaks.com/topic/118085-reading-in-string-an-extracting-item/#findComment-607526 Share on other sites More sharing options...
The Little Guy Posted August 4, 2008 Share Posted August 4, 2008 you may want to save the values in an array. Quote Link to comment https://forums.phpfreaks.com/topic/118085-reading-in-string-an-extracting-item/#findComment-607566 Share on other sites More sharing options...
2tonejoe Posted August 5, 2008 Author Share Posted August 5, 2008 Use some regex. This checks $string to see if it contains any _p followed by a number and if it does it shows you the captured number. <?php $string = 'johns-account_p4'; if(preg_match('/_p([0-9]{1,})/i',$string,$matches)) { print_r($matches); } else { echo 'Does not contain _p followed by a number'; } ?> This should help you get started, but I can't write anything complete because I'm not sure exactly how you store your strings, what you want to do with the captured number ect... The strings hold two names (john and sam) but the new strings only hold one name (john)... the strings are returned from a database and stored into a variable. The names don't mean anything really. Anything before the _p should be the prefix of the new strings . . for instances here are examples of inputs and the hoped output: input: $oldVar = johns-account_p8 output: $newVar1 = johns-account_p1 $newVar1 = johns-account_p2 $newVar1 = johns-account_p3 $newVar1 = johns-account_p4 $newVar1 = johns-account_p5 $newVar1 = johns-account_p6 $newVar1 = johns-account_p7 $newVar1 = johns-account_p8 input: $oldVar = sams-test-account_p8 output: $newVar1 = sams-test-account_p1 $newVar1 = sams-test-account_p2 $newVar1 = sams-test-account_p3 input: $oldVar = sams-test-account output: $newVar1 = sams-test-account does that help at all? Quote Link to comment https://forums.phpfreaks.com/topic/118085-reading-in-string-an-extracting-item/#findComment-608158 Share on other sites More sharing options...
2tonejoe Posted August 5, 2008 Author Share Posted August 5, 2008 bumb Quote Link to comment https://forums.phpfreaks.com/topic/118085-reading-in-string-an-extracting-item/#findComment-608368 Share on other sites More sharing options...
Wuhtzu Posted August 5, 2008 Share Posted August 5, 2008 It sort of helps, but I still can't figure you your system: Input: $oldVar = johns-account_p8 Output: $newVar1 = johns-account_p1 $newVar1 = johns-account_p2 $newVar1 = johns-account_p3 $newVar1 = johns-account_p4 $newVar1 = johns-account_p5 $newVar1 = johns-account_p6 $newVar1 = johns-account_p7 $newVar1 = johns-account_p8 Input: $oldVar = sams-test-account_p8 Output: $newVar1 = sams-test-account_p1 $newVar1 = sams-test-account_p2 $newVar1 = sams-test-account_p3 Why does "johns-account_p8" generate 8 new variables when "sams-test-account_p8" only generates 3 new variables? input: $oldVar = sams-test-account output: $newVar1 = sams-test-account And "sams-test-account" does not contain _p followed by a number at all but still he gets a new variable? What you want can easily be obtained, but I just need to know exactly what you want Quote Link to comment https://forums.phpfreaks.com/topic/118085-reading-in-string-an-extracting-item/#findComment-608448 Share on other sites More sharing options...
wildteen88 Posted August 5, 2008 Share Posted August 5, 2008 This how I'd do it: $str = 'johns-account_p5'; $arr = preg_split('/_p([0-9]+)$/i', $str, 0, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE); if(is_array($arr) && count($arr) == 2) { list($name, $pX) = $arr; for($i = 0; $i <= $pX; $i++) { ${'newVar'.$i} = $name . '_p' . $i; } echo $newVar1 . '<br />'; echo $newVar3 . '<br />'; echo $newVar5 . '<br />'; } else { $newVar1 = $str; } However you'll be better of using an array rather than series of variables named the same Quote Link to comment https://forums.phpfreaks.com/topic/118085-reading-in-string-an-extracting-item/#findComment-608478 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.