Page 1 of 1

returning a string up to a certain character.

Posted: Fri Sep 27, 2002 12:29 pm
by waskelton4
Hello again forum,
Most of those String manipulation functions in the manual are confusing the mess outta me.

what i'm trying to do is simply take a string and extract the begining part of that string out, up to a certian character "(".

i've tried a number of the functions and have been able to get everything after the "(" but not before..

currently i'm battling the preg_split() function becuase i think i'll be able to actually use more than just one part of the string..


Here is what i'm trying to do specifically....

take..

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"

and extract up to the first "(" and keep everything before and after that.. then take everything after the "(" and split it by the ";" and stor all the data in an array(i'm guessing that's the best way). please help..

the main reason that the manual confuses me so much is because most of the examples are using special characters and use so many escapes (that i don't totally understand) that it clouds up the example with extra characters that (i don't think) need to be there to get the point of the function across...

sheesh.. i need some more coffee..

any help is greatly appreciated..

Thanks
Will

Posted: Fri Sep 27, 2002 12:35 pm
by nielsene
Assuming you put the string to parse in $user_agent, this should get you started.

Code: Select all

// find the location of the open parenthesis
$openParenPos=strpos($user_agent,'(');

// place everything before the parenthesis into a string
$initialString = substr($user_agent,0,$openParenPos-1);

// spilt everything after the parentheis on the semi-colon deliminators
$commentComponents = explode(';',substr($initialString,$openParenPos+1));