Re: how to explode or split or chunk in an efficient way

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

 



Sincerely
Negin Nickparsa


On Thu, Jul 24, 2014 at 12:04 PM, Robert Cummings <robert@xxxxxxxxxxxxx>
wrote:

> On 14-07-24 02:49 PM, Robert Cummings wrote:
>
>> On 14-07-24 12:18 PM, Negin Nickparsa wrote:
>>
>>> I have a string which always has "C" then a number which it's length can
>>> change anytime and then "P" and then a space and then a word
>>>
>>> like:
>>>
>>> echo "C60P1 Unit";
>>>
>>
>> This doesn't match your description... there's a number after the 'P' :)
>>
>>  is it anyway to split the string by type?
>>>
>>> from the manual I did this:
>>>
>>> function multiexplode ($delimiters,$string) {
>>>       $ary = explode($delimiters[0],$string);
>>>       array_shift($delimiters);
>>>       if($delimiters != NULL) {
>>>           foreach($ary as $key => $val) {
>>>                $ary[$key] = multiexplode($delimiters, $val);
>>>           }
>>>       }
>>>       return  $ary;
>>> }
>>> echo "C60P1 Unit";
>>> $str="C60P1 Unit";
>>> $delimiters = Array("C","P"," ");
>>> $res = multiexplode($delimiters,$str);
>>> echo '<pre>';
>>> print_r($res);
>>> echo '</pre>';
>>>
>>> but it seems to me it is so complex for such a small string
>>> does anything for type exist which I don't know about to for example just
>>> grab the numbers?because I just care about the numbers here 60 and 1
>>> seperately
>>>
>>
>> <?php
>>
>> $str = 'C60P1 Unit';
>> $num = preg_replace( '#^[^[:digit:]]+([[:digit:]]+).*$#', '\1', $str );
>>
>
​@Robert wow it is complex version of cool :D thanks by the way​

>
>> ?>
>>
>
> Sorry, I missed that you want the 60 and the 1... I think others have done
> similarly but just to be different here's another take:
>
>
> <?php
>
> $str = 'C60P1 Unit';
>
> list( $num1, $num2 ) =
>     explode( ':', preg_replace( '#^[^[:digit:]]+([[:digit:]]+)
> [^[:digit:]]+([[:digit:]]+).*$#', '\1:\2', $str ) );
>
> echo $num1."\n";
> echo $num2."\n";
>
>
> ?>
>
> Cheers,
> Rob.
> --
> Phone: 613-822-9060    +++    Cell: 613-600-2836
> E-Mail Disclaimer: Information contained in this message and any
> attached documents is considered confidential and legally protected.
> This message is intended solely for the addressee(s). Disclosure,
> copying, and distribution are prohibited unless authorized.
>

[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