string issue

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
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

string issue

Post by itsmani1 »

there is a tring

$str = " Abc Def";

want to fetch Abc only, is there any php function that can help me, i want to fetch string before " "

thanx
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Is the first space in the string intentional (you have two)?

If not

Code: Select all

$str = "Abc Def"; 

$parts = explode(" ", $str);

echo $parts[0];

// outputs Abc
Last edited by JayBird on Mon Jan 23, 2006 10:36 am, edited 1 time in total.
User avatar
jasonjohnson
Forum Newbie
Posts: 6
Joined: Wed Jan 18, 2006 9:21 am
Location: Dallas, TX, USA

Post by jasonjohnson »

If you know how long that segment is going to be, you can use substr() -- or, if you know you will always have whitespace surrounding your segments per your example, you can use explode().

For instance:

Code: Select all

$str = " Abc Def";

echo substr($str, 1, 3);
Where $str is obviously the variable you're slicing up, 1 is the starting point, and 3 is the number of characters you expect to pull out of the string from the starting point forward.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

Code: Select all

mysql_query("select ContactID, FirstName, EmailAddress from Contacts where ContactID IN(select ContactID from TenantLocationsContacts where TenantLocationsID IN (select TenantLocationsID from TenantLocationsContacts where TenantLocationsID IN (select TenantLocationsID from TenantLocations where TenantID=1) GROUP BY TenantLocationsID HAVING count(TenantLocationsID) > 1))") or die("");
there is some issue with this? can any one help me?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

itsmani1 wrote:

Code: Select all

mysql_query("select ContactID, FirstName, EmailAddress from Contacts where ContactID IN(select ContactID from TenantLocationsContacts where TenantLocationsID IN (select TenantLocationsID from TenantLocationsContacts where TenantLocationsID IN (select TenantLocationsID from TenantLocations where TenantID=1) GROUP BY TenantLocationsID HAVING count(TenantLocationsID) > 1))") or die("");
there is some issue with this? can any one help me?
:? What did this have to do with the original post?

Change or die('') to `or die(mysql_error())'.

As to the first post you may want to look at regular expressions if the other two posters' comments haven't helped ;)
Post Reply