note 37229 added to function.substr

From: Date: Fri, 07 Nov 2003 07:08:42 +0000
Subject: note 37229 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-60045@lists.php.net to get a copy of this message
<?php #CORRECTION FOR THE PREVIOUS EXAMPLE #to get a substring between two known strings, to menepulate it ( split it into two parts) #for example: I have this string that have a city and a state, and I need to get the state and the city seperatly $myContent ="hey you , I live in Detroit Michigan, USA and I like it baby"; # not I am adding the space after "live in" because after I get my substring I will split it based on the space between the words $myStartSub = "live in "; $myEndSub =", USA"; $my_substr= substr($myContent,strpos($myContent,$myStartSub )+strlen($myStartSub), strpos($myContent,$myEndSub)); $my_substr= substr($my_substr,0, strpos($my_substr,$myEndSub)); echo " the substring : ".$my_substr; # the end result would be # myEndSub = "Detroit Michigan" #now after we got the string that have the city and state, we can start choping it to two variables $firstSection = substr($my_substr,0, strpos($my_substr," ")); echo "<br> the City :".$firstSection; $SecoundSection= substr($my_substr,strpos($my_substr," " )+strlen(" ")); echo "<br> the state :".$SecoundSection; #the resutl would be like this : //---------->the substring : Detroit Michigan //---------->the City :Detroit //---------->the state :Michigan # ofcourse the last part ( splitting of the string could be done in many other ways, but hey if you know it will only be city and state, then that would work ofcourse unless the sity is san fransisco, or the state is new orliens ?> ---- Manual Page -- http://www.php.net/manual/en/function.substr.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37229 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37229&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37229&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#60045) next »