Discussion:
Finding the Middle elements of a String
dealTek
2014-05-11 18:01:17 UTC
Permalink
Hi all,

Let's say the string is "apple,cherry,peach,pear" (not an array just a text string)

I see that I can use these (strpos & strripos) to search for the first or last "," - but how to I search for the ones in the middle (like third comma or 4th comma)?

strpos — Find the position of the first occurrence of a substring in a string

strripos — Find the position of the last occurrence of a case-insensitive substring in a string

Thanks in advance

ex:

$this1 = "apple,cherry,peach,pear";
$findme = ',';
$pos = strpos($this1, $findme);
$pos2 = strripos($this1, $findme);


$showfirst = substr($this1, 0, $pos);
$showlast = substr($this1, $pos2+1, 9999 );










--
Thanks,
Dave - DealTek
***@gmail.com
[db-14]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daniel Brown
2014-05-11 18:04:42 UTC
Permalink
Post by dealTek
Hi all,
Let's say the string is "apple,cherry,peach,pear" (not an array just a text string)
I see that I can use these (strpos & strripos) to search for the first or last "," - but how to I search for the ones in the middle (like third comma or 4th comma)?
strpos — Find the position of the first occurrence of a substring in a string
strripos — Find the position of the last occurrence of a case-insensitive substring in a string
Thanks in advance
$this1 = "apple,cherry,peach,pear";
$findme = ',';
$pos = strpos($this1, $findme);
$pos2 = strripos($this1, $findme);
$showfirst = substr($this1, 0, $pos);
$showlast = substr($this1, $pos2+1, 9999 );
Consider using explode().
--
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ken Robinson
2014-05-11 18:12:15 UTC
Permalink
What are you really looking to do? If you're
looking to get the words out of the string, just
explode it into an array and get the elements you want from the array.

$str = "apple,cherry,peach,pear";
$ary = explode(',',$str);

Ken
Post by dealTek
Hi all,
Let's say the string is
"apple,cherry,peach,pear" (not an array just a text string)
I see that I can use these (strpos & strripos)
to search for the first or last "," - but how to
I search for the ones in the middle (like third comma or 4th comma)?
strpos — Find the position of the first occurrence of a substring in a string
strripos — Find the position of the last
occurrence of a case-insensitive substring in a string
Thanks in advance
$this1 = "apple,cherry,peach,pear";
$findme = ',';
$pos = strpos($this1, $findme);
$pos2 = strripos($this1, $findme);
$showfirst = substr($this1, 0, $pos);
$showlast = substr($this1, $pos2+1, 9999 );
--
Thanks,
Dave - DealTek
[db-14]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-05-11 18:15:23 UTC
Permalink
What are you really looking to do? If you're looking to get the words out
of the string, just explode it into an array and get the elements you want
from the array.
$str = "apple,cherry,peach,pear";
$ary = explode(',',$str);
Ken
Post by dealTek
Hi all,
Let's say the string is "apple,cherry,peach,pear" (not an array just a text string)
I see that I can use these (strpos & strripos) to search for the first or
last "," - but how to I search for the ones in the middle (like third comma
or 4th comma)?
strpos — Find the position of the first occurrence of a substring in a
string
strripos — Find the position of the last occurrence of a case-insensitive
substring in a string
Thanks in advance
$this1 = "apple,cherry,peach,pear";
$findme = ',';
$pos = strpos($this1, $findme);
$pos2 = strripos($this1, $findme);
$showfirst = substr($this1, 0, $pos);
$showlast = substr($this1, $pos2+1, 9999 );
--
Thanks,
Dave - DealTek
[db-14]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
If you don't want to use an array:

$string = 'apple,cherry,peach,pear';
$offset = 0;
while ($offset = strpos($string, ',', $offset+1)) {
echo 'Comma @ offset: ' . $offset . '<br />';
}
dealTek
2014-05-11 20:19:57 UTC
Permalink
What are you really looking to do? If you're looking to get the words out of the string, just explode it into an array and get the elements you want from the array.
$str = "apple,cherry,peach,pear";
$ary = explode(',',$str);
Ken
THANKS EVERYBODY - SUPER HELPFUL!

Yes I wanted to be able to pull out various elements as listed below - so this works great

<?php
$str = "apple,cherry,peach,pear";
$ary = explode(',',$str);


// use for various needs...
echo $ary[0].' - ' .$ary[1].' - ' .$ary[2].' - ' .$ary[3].' - ' .$ary[4];

?>






--
Thanks,
Dave - DealTek
***@gmail.com
[db-14]

Jigar Dhulla
2014-05-11 19:01:28 UTC
Permalink
Hi,

As long as I know you can consider a string as array in php. Means
$string[0] is "a", $string[1] is "p" and so on. So you can use a foreach
like :

foreach($string as $k => $v){
if($v == ",") $pos[] = $k;
}

$pos will be an array with values of commas position.

Note: index starts with 0. So $pos[0] will be 5 and so on.

Please correct me if I am wrong. I have not tested this or used this method
anywhere.
Post by dealTek
Hi all,
Let's say the string is "apple,cherry,peach,pear" (not an array just a text string)
I see that I can use these (strpos & strripos) to search for the first or
last "," - but how to I search for the ones in the middle (like third comma
or 4th comma)?
strpos — Find the position of the first occurrence of a substring in a
string
strripos — Find the position of the last occurrence of a case-insensitive
substring in a string
Thanks in advance
$this1 = "apple,cherry,peach,pear";
$findme = ',';
$pos = strpos($this1, $findme);
$pos2 = strripos($this1, $findme);
$showfirst = substr($this1, 0, $pos);
$showlast = substr($this1, $pos2+1, 9999 );
--
Thanks,
Dave - DealTek
[db-14]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
2014-05-11 19:06:18 UTC
Permalink
Please don't do this, it *will* break on multibyte strings, as those "arrays" are not based on characters but bytes.
Post by Jigar Dhulla
Hi,
As long as I know you can consider a string as array in php. Means
$string[0] is "a", $string[1] is "p" and so on. So you can use a foreach
foreach($string as $k => $v){
if($v == ",") $pos[] = $k;
}
$pos will be an array with values of commas position.
Note: index starts with 0. So $pos[0] will be 5 and so on.
Please correct me if I am wrong. I have not tested this or used this method
anywhere.
Post by dealTek
Hi all,
Let's say the string is "apple,cherry,peach,pear" (not an array just
a
Post by dealTek
text string)
I see that I can use these (strpos & strripos) to search for the
first or
Post by dealTek
last "," - but how to I search for the ones in the middle (like third
comma
Post by dealTek
or 4th comma)?
strpos — Find the position of the first occurrence of a substring in
a
Post by dealTek
string
strripos — Find the position of the last occurrence of a
case-insensitive
Post by dealTek
substring in a string
Thanks in advance
$this1 = "apple,cherry,peach,pear";
$findme = ',';
$pos = strpos($this1, $findme);
$pos2 = strripos($this1, $findme);
$showfirst = substr($this1, 0, $pos);
$showlast = substr($this1, $pos2+1, 9999 );
--
Thanks,
Dave - DealTek
[db-14]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Thanks,
Ash
Loading...