asaschool Posted September 17, 2008 Share Posted September 17, 2008 Hey all, I have a situation where I need to extract a name from a query string. Normally I would use substr() to extract the data I need but this is a different situation. I have a database with data such as this: name (id number) phone - fax namename (id number) phone - cell - fax They are different with every row and I need to extract just the data (name) up until the beginning of the "(id number)" part. I know there is a way to do this by telling php to give me all of the data up to the "(", but I cannot figure out how to do this. Can anybody help? Thanks in advance Link to comment https://forums.phpfreaks.com/topic/124574-help-extracting-data-from-a-string/ Share on other sites More sharing options...
genericnumber1 Posted September 17, 2008 Share Posted September 17, 2008 can you give me an example string along with what you want from that string? Such as... alfkn!~3flk~!sldfk "I want the 3flk" Just trying to be 100% sure I know what you want EDIT: I think what you want is $str = 'namename (id number) phone - cell - fax'; $pos = strpos($str, '('); $name = trim(substr($str, 0, $pos)); echo $name; // echos namename Link to comment https://forums.phpfreaks.com/topic/124574-help-extracting-data-from-a-string/#findComment-643424 Share on other sites More sharing options...
asaschool Posted September 17, 2008 Author Share Posted September 17, 2008 Thank you for such a quick response. Here is an example of what I need: Johnny Newguy (ID:555555555) Office:555-555-5551 Mike Honcho (ID:666666666) Cellular:555-555-5552 Office:555-555-5553 Seamore Buttz (ID:3333333333) Primary:555-555-5554 Secondary:555-555-5555 Other:555-555-5556 What I need to be able to do is extract just their names from the string. So in this case I need to be able to display: Johnny Newguy, Mike Honcho, and Seamore Buttz. I tried to use the "(" as my identifier of where to stop the extraction, but have been unable to get it to work properly. Thanks again. Link to comment https://forums.phpfreaks.com/topic/124574-help-extracting-data-from-a-string/#findComment-643428 Share on other sites More sharing options...
genericnumber1 Posted September 17, 2008 Share Posted September 17, 2008 Okay, cool, thanks. Then see what I posted above thanks again for clarifying. I come on here after classes so my brain is fried, I often misunderstand people. EDIT: I should also say, before someone else points it out, that $str = 'namename (id number) phone - cell - fax'; $name = trim(strstr($str, '(', true)); echo $name; // echos namename should also work, but you need to have a version of php past 5.3, so the first might be best for legacy systems/portability. Link to comment https://forums.phpfreaks.com/topic/124574-help-extracting-data-from-a-string/#findComment-643429 Share on other sites More sharing options...
asaschool Posted September 17, 2008 Author Share Posted September 17, 2008 Thats it! I guess I should of stopped trying to make substr work and try other syntax's. Thanks again I really appreciate it. Link to comment https://forums.phpfreaks.com/topic/124574-help-extracting-data-from-a-string/#findComment-643434 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.