Jump to content

help extracting data from a string


asaschool

Recommended Posts

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

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

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.

 

Okay, cool, thanks. Then see what I posted above :D 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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.