ballouta Posted August 24, 2008 Share Posted August 24, 2008 Hello I have a string on this format: sentence one goes here (1) sentence two goes here (2) etc... I am looking for an expression inorder to split each sentence so I can insert it in a field in a DB separately,, the output should look like: sentence one goes here sentence two goes here (I will put the insertion code here for each sentence) Thanks in advance Quote Link to comment Share on other sites More sharing options...
ballouta Posted August 24, 2008 Author Share Posted August 24, 2008 any help please Quote Link to comment Share on other sites More sharing options...
ibechane Posted August 24, 2008 Share Posted August 24, 2008 How much regex do you know? The php function I would use is preg_match_all. Quote Link to comment Share on other sites More sharing options...
ballouta Posted August 24, 2008 Author Share Posted August 24, 2008 Sorry i do NOT have any experience in these functions, but i asked similar question before and I got an answer. Kindly those who have experience help me thanks alot Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 24, 2008 Share Posted August 24, 2008 I think it would help to have an actual sample string to examine.. Using preg_match_all, one would need to know where to break the sentence up into captured chunks. One of the big problem sacross these forums is poor initial communication. Some people will list a bunch of sentences stacked ontop of one another (which could leads others to think about arrays, only to find out it was all in one long string). Being clear and showing examples from the get-go is certainly a faster way of getting some help as opposed to half-assed explanations which cause wasted time and confusion. On the topic of getting help, I think it wouldn't hurt to look up beginner tutorials on regex on the net. While getting the fast and easy answers does eliminate any work on your end, it doesn't further improve yourself. Even a basic knowledge of stuff on regex can go a long way in setting up the foundation to answers you need. After all, the more you know, the less dependant you will be on others. It's just a suggestion. I help out if I can (or feel like it) for the practise sake of it.. otherwise, I would leave these people with the issues they have. Quote Link to comment Share on other sites More sharing options...
ibechane Posted August 25, 2008 Share Posted August 25, 2008 I think what ballouta wants is to separate a string into chunks using ([0-9]*) as the separator. It's really not that hard. All you need to know is how regex works and how preg_match_all stores back references. There are tons of regex sites with helpful examples. Learn to use google. The php docs are one the best online docs for a programming language - http://us2.php.net/preg_match_all There's a difference between asking for programming help, and hoping someone will just do it for free. I can't speak for everyone here, but I'm not going to just give you a full answer. If you show some work, ask nicely, and have showed that you've really tried and are stuck, then I would be more than glad to help. If you're not willing to learn, or show any sign of effort, then hire a programmer. On a related note, I'm available for hire. Quote Link to comment Share on other sites More sharing options...
effigy Posted August 25, 2008 Share Posted August 25, 2008 preg_match_all is not the answer, at least from my understanding of the post. <pre> <?php $str = 'sentence one goes here (1) sentence two goes here (2) etc...'; print_r(preg_split('/\s*\(\d+\)\s*/', $str)); ?> </pre> Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 25, 2008 Share Posted August 25, 2008 I was thinking along the lines of: preg_match_all('#([-a-z\s.?!])+#i', $str, $matches); // allow for extra characters in the sentences //then echo out $matches[0] in foreach loop It would work (on condition that the sentences don't contain any characters not listed in the character class)..But admittedly not nearly as flexible nor streamlined as your solution. On a side note, I picked up 'Mastering Regular Expressions' yesterday and am going through it chapter by chapter.. only on chapter 2 and already some things are getting clearer.. so this book looks very promising. By the end of it, I should be far more knowledgable with regex. :-\ Quote Link to comment Share on other sites More sharing options...
ballouta Posted August 27, 2008 Author Share Posted August 27, 2008 Hi I prefer the preg written by nrg_alpha since the output is exactly as I want Note: the sentences in my situation will never contain the characters ) or ( so don't worry Many Thanks Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 27, 2008 Share Posted August 27, 2008 Note: the sentences in my situation will never contain the characters ) or ( so don't worry Ah.. well, that changes things.. had your sentences had (1), (2) etc.. effigy's method would be the best one. This is why I ask people to give an ACTUAL example of what they have to work with.. given what you mentioned, your example: sentence one goes here (1) sentence two goes here (2) etc... is not correct as it has ( and ) characters in it. When it boils down to examples to be used in regular expressions, it is very important to have crsytal clear examples and an equally clear goal desciption..simply because regular expressions is such a strict 'mini-language', there is no room for errors. Quote Link to comment Share on other sites More sharing options...
effigy Posted August 27, 2008 Share Posted August 27, 2008 I prefer the preg written by nrg_alpha since the output is exactly as I want It only covers -, a-z, white space, ., ?, and !. Is that enough? No one is going to throw in @, #, $, %, &, *, etc? The parens are not needed: #[-a-z\s.?!]+#i. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.