Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/118085-reading-in-string-an-extracting-item/
Share on other sites

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)...

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.

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?

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 :)

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.