Re: Finding the Middle elements of a String
| From: | Ken Robinson | Date: | Sun, 11 May 2014 18:12:15 +0000 |
| Subject: | Re: Finding the Middle elements of a String | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-323278@lists.php.net to get a copy of this message | ||
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
At 02:01 PM 5/11/2014, dealTek wrote:
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 dealtek@gmail.com [db-14] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php