siddscool19 Posted September 9, 2008 Share Posted September 9, 2008 I want to know how to get the first line of the file for example this is the file I am a good boy 23. I am a good boy 34. I am a good boy 24. I am a good boy 21. I am a good boy 45. Now how could i get only the first line in match taking the case there is only a slight change in the lines and that we want to figure out. The numbers in the above case. I just want to get the first line and stop the function to get further lines. In the above case i don't know the number of first line which I want to know. Which i can easily get to know using (.*) . But the thing is that how to get only the first line and not others. What codes i have to use? Quote Link to comment https://forums.phpfreaks.com/topic/123446-get-the-first-common-line-of-a-file-using-preg_match/ Share on other sites More sharing options...
JonnoTheDev Posted September 9, 2008 Share Posted September 9, 2008 Your post doesnt really make sense. Are you trying to extract the first line of the file i.e I am a good boy 23. Or the numeric value within the first line i.e. 23 or something else? Quote Link to comment https://forums.phpfreaks.com/topic/123446-get-the-first-common-line-of-a-file-using-preg_match/#findComment-637564 Share on other sites More sharing options...
siddscool19 Posted September 9, 2008 Author Share Posted September 9, 2008 We can say like that I want to use this coding $content= "I am a good boy 23. I am a good boy 34. I am a good boy 24. I am a good boy 21. I am a good boy 45." preg_match("/I am a good boy (.*)/",$content,$number); echo $number[1]; Now the output which i get should be 23 not any other number how to do that? Quote Link to comment https://forums.phpfreaks.com/topic/123446-get-the-first-common-line-of-a-file-using-preg_match/#findComment-637629 Share on other sites More sharing options...
JonnoTheDev Posted September 9, 2008 Share Posted September 9, 2008 Incorrect. Use: $subject = "I am a good boy 34."; preg_match('/([0-9]+)/', $subject, $match); echo $match[1]; Quote Link to comment https://forums.phpfreaks.com/topic/123446-get-the-first-common-line-of-a-file-using-preg_match/#findComment-637631 Share on other sites More sharing options...
discomatt Posted September 9, 2008 Share Posted September 9, 2008 That code should output '23.' To get only the number (no ending period), simply use preg_match("/I am a good boy (\d++)/",$content,$number); Quote Link to comment https://forums.phpfreaks.com/topic/123446-get-the-first-common-line-of-a-file-using-preg_match/#findComment-637634 Share on other sites More sharing options...
discomatt Posted September 9, 2008 Share Posted September 9, 2008 Incorrect. Use: $subject = "I am a good boy 34."; preg_match('/([0-9]+)/', $subject, $match); echo $match[1]; This might run into error if you use something like $content= "28 brown cows 23. 28 brown cows 34. 28 brown cows 24. 28 brown cows 21. 28 brown cows 45."; Quote Link to comment https://forums.phpfreaks.com/topic/123446-get-the-first-common-line-of-a-file-using-preg_match/#findComment-637636 Share on other sites More sharing options...
JonnoTheDev Posted September 9, 2008 Share Posted September 9, 2008 What the hell? $content= "28 brown cows 23. 28 brown cows 34. 28 brown cows 24. 28 brown cows 21. 28 brown cows 45."; This is an example: $subject = "I am a good boy 34."; preg_match('/([0-9]+)/', $subject, $match); It obviously needs to be modified so each line that is read in from the file is passed into the regex to extract the number. $subject is used as an example Quote Link to comment https://forums.phpfreaks.com/topic/123446-get-the-first-common-line-of-a-file-using-preg_match/#findComment-637647 Share on other sites More sharing options...
effigy Posted September 9, 2008 Share Posted September 9, 2008 If the file is in a Unix format: <pre> <?php $data = <<<DATA I am a good boy 23. I am a good boy 34. I am a good boy 24. I am a good boy 21. I am a good boy 45. DATA; preg_match('/(\d+)\.$/m', $data, $matches); print_r($matches); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/123446-get-the-first-common-line-of-a-file-using-preg_match/#findComment-637663 Share on other sites More sharing options...
siddscool19 Posted September 10, 2008 Author Share Posted September 10, 2008 What I mean is that I have variable which contains stored text from a textarea form. Now i want to get number from the middle or only first particular line. How to tell preg_match to get me a particular line only rather searching all the lines? All the lines are similar except their numbers. Quote Link to comment https://forums.phpfreaks.com/topic/123446-get-the-first-common-line-of-a-file-using-preg_match/#findComment-638011 Share on other sites More sharing options...
effigy Posted September 10, 2008 Share Posted September 10, 2008 <pre> <?php $data = <<<DATA I am a good boy 23. I am a good boy 34. I am a good boy 24. I am a good boy 21. I am a good boy 45. DATA; ### 0-indexed. $which_one = 4; print_r(array_splice(explode("\n", $data), $which_one, 1)); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/123446-get-the-first-common-line-of-a-file-using-preg_match/#findComment-638383 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.