Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to regex first line of text 1

Status
Not open for further replies.

pushyr

Programmer
Jul 2, 2007
159
GB
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);
 
Hi

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


Feherke.
[link feherke.github.com/][/url]
 
ahh.. ok, i think i know were i went wrong with my initial question. i'm really trying to get the second line...

----------------------------------------------------------

GET /news/ HTTP/1.1
Host: 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: 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%25252F s_sv_sid=914755482672; _em_vt=8c24acf5b8ae7b4f2d369c1c4b765016677ce12930-106766535016a7a1; _em_hl=1; tr_pr1=bbc%20news%20%7C%20uk~%5B2%5D; c=undefined 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:
 
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?
 
Hi

Yes, [tt]explode()[/tt]ing that should not be so big effort.

In case you have PHP 5.4, you can write it shorter as :
Code:
[navy]$url[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]explode[/color][teal]([/teal][green][i]"\r\n"[/i][/green][teal],[/teal] [navy]$content00[/navy][teal])[[/teal][purple]1[/purple][teal]];[/teal]

As extra caution I would think about splitting on [tt]\n[/tt] and removing the remaining [tt]\r[/tt] with [tt]rtrim()[/tt] :
PHP:
[navy]$url0[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]explode[/color][teal]([/teal][green][i]"\n"[/i][/green][teal],[/teal] [navy]$content00[/navy][teal]);[/teal]
[navy]$url[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]rtrim[/color][teal]([/teal][navy]$url0[/navy][teal][[/teal][purple]1[/purple][teal]],[/teal] [green][i]"\r"[/i][/green][teal]);[/teal]
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.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top