Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...If I'd only had resource like eng-tips when I was just getting started! I might have dazzled them with my brilliance instead of my BS..."

Geography

Where in the world do Tek-Tips members come from?

trying to regex first line of textHelpful Member! 

pushyr (Programmer)
30 Jul 12 13:28
this is perhaps one of the simplest things to do, but after googling i just can't find anything that works. Here's my own concoction, which doesn't work.
...so, how would i regex the first line from a block of text

CODE

preg_match("#^(.*?)\r\n#is", $content00); 
feherke (Programmer)
30 Jul 12 14:09
Hi

First of all, this sounds like a bad idea. Regexes are slower that plain text manipulation functions.
  • If $content00 is short, I would use array_shift(explode("\r\n",$content00,2));.
  • If $content00 is long, I would use substr($content00,0,($pos=strpos($content00,"\r\n"))!==false?$pos:strlen($content00));.
By the way, your code works for me. Show us some sample $content00 value and usage example.

Feherke.
http://feherke.github.com/

pushyr (Programmer)
30 Jul 12 14:41
ahh.. ok, i think i know were i went wrong with my initial question. i'm really trying to get the second line...

Quote:


----------------------------------------------------------
http://www.bbc.co.uk/news/

GET /news/ HTTP/1.1
Host: www.bbc.co.uk
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://www.google.co.uk/url?sa=t&rct=j&q=&...
Cookie: NTABS=B8; BBC-UID=4580115552e46478bdcebce241a7cd2b814fb7e3aed8e3f93365dabd39fba7900Mozilla%2f5%2e0%20%28Macintosh%3b%20Intel%20Mac%20OS%20X%2010%2e7%3b%20rv%3a14%2e0%29%20Gecko%2f20100101%20Firefox%2f14%2e0%2e1; ed=1; BGUID=c580a1a5d234e419ea78e67681c3d99953fe069cfe9823098325aa9d791ea520; ckns_policy=111; s1=5015245155C80241; rsi_segs=J08781_10053|J08781_10052|J08781_10058|J08781_10132|J08781_10338|J08781_10340|J08781_10342|J08781_10362|J08781_10398|J08781_10417|J08781_10572|J08781_10608|J08781_10609|J08781_10610|J08781_10611; s_cc=true; s_vnum=1346237563852%26vn%3D2; s_nr=1343645563853; s_ev48=%5B%5BB%5D%5D; s_sq=bbcwglobalprod%3D%2526pid%253Dbbc%252520news%252520%25257C%252520uk%2526pidt%253D1%2526oid%253Dhttp%25253A%25252F%25252Fwww.bbc.co.uk%25252Fnews%25252Ftechnology%25252F%2...; s_sv_sid=914755482672; _em_vt=8c24acf5b8ae7b4f2d369c1c4b765016677ce12930-106766535016a7a1; _em_hl=1; tr_pr1=bbc%20news%20%7C%20uk~%5B2%5D; c=undefinedwww.google.ptwww.google.pt; s_ev49=%5B%5B'Other%2520Referrers'%2C'1343660905153'%5D%5D

HTTP/1.1 200 OK
Date: Mon, 30 Jul 2012 18:37:46 GMT
Server: Apache
Vary: Cookie,X-CDN
Cache-Control: max-age=0
Expires: Mon, 30 Jul 2012 18:37:46 GMT
Access-Control-Allow-Origin: *
Keep-Alive: timeout=5, max=758
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
----------------------------------------------------------

so the first line is: ----------------------------------------------------------
and the second line i'm after is: http://www.bbc.co.uk/news/
pushyr (Programmer)
30 Jul 12 15:17
thanks for your help feherke... i solved it with this...

$url0 = explode("\r\n", $content00);
$url = $url0[1];

since the text will always be 20 to 30 lines, i think this is ok?
Helpful Member!  feherke (Programmer)
31 Jul 12 2:31
Hi

Yes, explode()ing that should not be so big effort.

In case you have PHP 5.4, you can write it shorter as :

CODE --> PHP-5.4

$url = explode("\r\n", $content00)[1]; 

As extra caution I would think about splitting on \n and removing the remaining \r with rtrim() :

CODE --> PHP

$url0 = explode("\n", $content00);
$url = rtrim($url0[1], "\r"); 
Of course, if you can be sure about the DOS line endings, this is unnecessary. ( If you also want compatibility with MacOS line endings too, that will get back to regular expressions. )

Feherke.
http://feherke.github.com/

pushyr (Programmer)
31 Jul 12 7:47
cheers feherke!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close