|
-
Jul 17th, 2009, 09:18 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Help parse string
$description = ' "klsfdsfasdfasdf" slsjfklasdf "kjsfklasdjflaksfja" ';
What I would like to do is to finish off with a string of only the first group of brackets. So everything after that is to be discarded.
I can use 'substr' to begin from the 1st letter (not the 0th as that is the first ") but how can I make it continue on until the NEXT "?
eg.
$d = substr($description,1,?);
-
Jul 17th, 2009, 09:39 AM
#2
Re: Help parse string
Actually, because you have whitespace at the start of the string, the character at position 1 is ", because position 0 is the whitespace. So the first thing you'd want to do is trim(), and then you could use substr() with the help of strpos(), like so:
Code:
$description = trim($description);
$d = substr($description,1,strpos($description,'"',1)-1);
strpos($description,'"',1)-1 will return the position of the second ", minus one, which is the length of the sub string you want.
I tend to prefer regex for string parsing though - it's usually simpler and more powerful. Using preg_match(), you'd do this:
Code:
preg_match('/"([^"]*)"/',$description,$matches);
Your matched string is now is $matches[1] ($matches[0] contains the string too, but with " on either side). I can explain this example in more detail if you'd like.
-
Jul 17th, 2009, 10:02 AM
#3
Thread Starter
Hyperactive Member
Re: Help parse string
Excellent, thank you! I used the preg_match method and it worked great
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|