Discussion:
how to explode or split or chunk in an efficient way
Negin Nickparsa
2014-07-24 16:18:44 UTC
Permalink
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";

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
Jim Giner
2014-07-24 17:18:11 UTC
Permalink
Your problem is best solved with regex. I'm not a user of such myself,
but someone will come along shortly I'm sure.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Negin Nickparsa
2014-07-24 17:21:07 UTC
Permalink
Sincerely
Negin Nickparsa
Post by Jim Giner
Your problem is best solved with regex. I'm not a user of such myself,
but someone will come along shortly I'm sure.
​Jomali yeah! yours is nice too
Jim I solved it with guidance of Jeffery! and yes preg_match thank you!

preg_match('/([a-z]+)(\d+)([a-z]+)(\d+)/i', $str, $matches);​
Post by Jim Giner
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
jomali
2014-07-24 17:19:00 UTC
Permalink
One method is to use regular expressions e.g.:
$pattern = '/C([0-9]+)P/';
preg_match($pattern, $str, $matches);

$matches is an array. $matches[1] contains the number you want.
Post by Negin Nickparsa
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
echo "C60P1 Unit";
is it anyway to split the string by type?
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
Shawn McKenzie
2014-07-24 17:44:04 UTC
Permalink
preg_match_all('/\d+/', $str, $matches);

print_r($matches);

Or to make sure the delimiters are there:

preg_match('/C(\d+)P(\d+)/', $str, $matches);
Post by Negin Nickparsa
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
echo "C60P1 Unit";
is it anyway to split the string by type?
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
Negin Nickparsa
2014-07-24 17:54:32 UTC
Permalink
​yeah I did this one myself:
preg_match('/([a-z]+)(\d+)([a-z]+)(\d+)/i', $str, $matches);
​


Sincerely
Negin Nickparsa
Post by Shawn McKenzie
preg_match_all('/\d+/', $str, $matches);
print_r($matches);
preg_match('/C(\d+)P(\d+)/', $str, $matches);
Post by Negin Nickparsa
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
echo "C60P1 Unit";
is it anyway to split the string by type?
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
Jim Lucas
2014-07-24 18:31:30 UTC
Permalink
Post by Negin Nickparsa
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
echo "C60P1 Unit";
is it anyway to split the string by type?
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
Try this

<?php

$in = "C60P1 Unit";
$regex = "/C(?P<C>[0-9]+)P(?P<P>[0-9]+) (?P<D>.*)/";
preg_match($regex, $in, $m);
print_r($m);

?>

Array
(
[0] => C60P1 Unit
[C] => 60
[1] => 60
[P] => 1
[2] => 1
[D] => Unit
[3] => Unit
)
--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
2014-07-24 18:49:36 UTC
Permalink
Post by Negin Nickparsa
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
echo "C60P1 Unit";
This doesn't match your description... there's a number after the 'P' :)
Post by Negin Nickparsa
is it anyway to split the string by type?
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 );

?>

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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
2014-07-24 19:04:16 UTC
Permalink
Post by Robert Cummings
Post by Negin Nickparsa
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
echo "C60P1 Unit";
This doesn't match your description... there's a number after the 'P' :)
Post by Negin Nickparsa
is it anyway to split the string by type?
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 );
?>
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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Negin Nickparsa
2014-07-24 20:06:44 UTC
Permalink
Sincerely
Negin Nickparsa
Post by Robert Cummings
Post by Negin Nickparsa
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
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?
Post by Negin Nickparsa
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 );
?>
Sorry, I missed that you want the 60 and the 1... I think others have done
<?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.
Loading...