Re: stripping first comma off and everything after

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Jun 19, 2010 at 3:08 AM, Adam Richardson <simpleshot@xxxxxxxxx>wrote:

> On Fri, Jun 18, 2010 at 3:56 PM, Adam Williams <
> adam_williams@xxxxxxxxxxxxx> wrote:
>
>> I'm querying data and have results such as a variable named
>> $entries[$i]["dn"]:
>>
>> CN=NTPRTPS3-LANIER-LD335c-LH107-PPRNP9A92,OU=XXXXXXXXXXf,OU=XXXXXXXXXX,OU=XXXXXXXXXXXXX,DC=xxxxxxxx,DC=xxxxxxxxxx,DC=xxx
>>
>>
>> Basically I need to strip off the first command everything after, so that
>> I just have it display CN=NTPRTPS3-LANIER-LD335c-LH107-PPRNP9A92.
>>
>> I tried echo rtrim($entries[$i]["dn"],","); but that doesn't do anything.
>>  Any ideas?
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
> Adam (how could I not offer feedback to one with such a distinguished first
> name),
>
> rtrim() removes the characters contained in the second argument, it doesn't
> split a string using them.
>
> I would probably use strstr() if I didn't need the other sections, or, if I
> needed the other sections for later, I'd use explode:
>
> $your_string =
> 'CN=NTPRTPS3-LANIER-LD335c-LH107-PPRNP9A92,OU=XXXXXXXXXXf,OU=XXXXXXXXXX,OU=XXXXXXXXXXXXX,DC=xxxxxxxx,DC=xxxxxxxxxx,DC=xxx,';
>
> echo strstr($haystack = $your_string, $needle = ',', $before_needle =
> true);
>
> if ($sections = explode($delimiter = ',', $string = $your_string)) echo
> current($sections);
>
> Adam
>
> --
> Nephtali:  PHP web framework that functions beautifully
> http://nephtaliproject.com
>

Whoops!

I realized in the explode example I had omitted the call to count (idea
being if you didn't find any comma's, maybe you need to handle those
situations differently):

if (count($sections = explode($delimiter = ',', $string = $your_string)) >
1) echo $sections[0];

Although, if it doesn't matter, you could just do:

echo current(explode(',', $your_string));

And, as mentioned above, strstr() is one simple call if you won't need the
other sections:

echo strstr($your_string, ',' true);

Adam

-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux