br0k3n Posted August 16, 2007 Share Posted August 16, 2007 Say I have a string, for example: "Name: bob, Age: 22". I know that there is a name stored in there and that it is in that format. How can I trim that string down until it's left with just the name? Link to comment https://forums.phpfreaks.com/topic/65287-finding-a-value-within-a-string/ Share on other sites More sharing options...
lemmin Posted August 16, 2007 Share Posted August 16, 2007 $stoppos = strpos($string, ",", 6); $name = substr($string, 6, $stoppos-6); The 6 in the strpos() function is unnecessary, but it is also unnecessary to start searching from anywhere before that position. Link to comment https://forums.phpfreaks.com/topic/65287-finding-a-value-within-a-string/#findComment-326030 Share on other sites More sharing options...
br0k3n Posted August 17, 2007 Author Share Posted August 17, 2007 $stoppos = strpos($string, ",", 6); $name = substr($string, 6, $stoppos-6); The 6 in the strpos() function is unnecessary, but it is also unnecessary to start searching from anywhere before that position. Sorry, forgot to mention... The characters before and after that snippet can be infinite... When I meant that I knew the format, I meant that I knew that there is a "Name: bob" somewhere in the string, but it is not known where; therefore, strpos would not work. Link to comment https://forums.phpfreaks.com/topic/65287-finding-a-value-within-a-string/#findComment-326495 Share on other sites More sharing options...
jitesh Posted August 17, 2007 Share Posted August 17, 2007 $str = "Name: bob, Age: 22"; $all_a = explode(",",$str); $name_a = explode(": ",$all_a[0]); echo $name_a[1]; Link to comment https://forums.phpfreaks.com/topic/65287-finding-a-value-within-a-string/#findComment-326497 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.