returning a string up to a certain character.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
waskelton4
Forum Contributor
Posts: 132
Joined: Mon Sep 09, 2002 6:42 pm

returning a string up to a certain character.

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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));
Post Reply