![]() |
|
#1
|
|||
|
|||
preg_replace + regular expressionHi,
I've got a flat file such as: Quote:
I would like to retrieve the value of the parameters, i.e. have $title='Thursday 3rd March' $user='Paul Smith' $address='3, battersea square - SW12 3GE London - UK' So I tried the regular expression such as: $title=preg_replace("'<TITLE[^>]*?>.*?</TITLE>'", "\\1", $file); But it gives me a result such as:<USER>Paul Smith</USER><ADDRESS>3, battersea square - SW12 3GE London - UK</ADDRESS> Anybody can help? Thanks in advance |
|
#2
|
||||
|
||||
Use preg_match() instead?The question is: why are you using preg_replace() when it isn't obvious that you want to replace anything?
Use preg_match() or preg_match_all() instead: e.g. PHP Code:
On the other hand, I wouldn't store my data like that in a text file. __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#3
|
|||
|
|||
|
Ok ok
You are right I should use preg_match. I've never used it before so I was not familiar with. Thanks for the solution again. Cyril PS: Actually, as I'm thinking about that. Can you explain me the difference between PHP Code:
and PHP Code:
It gives exactly the same result (second one is an example I found from another forum) |
|
#4
|
||||
|
||||
PCRE pattern explanationActually Cyril, there's no difference...
this bit: Code:
![]() Another is that the pattern I supplied earlier should work even if you had inadvertently stored some data like this (note the lowercase tags for 'title'): Code:
i (PCRE_CASELESS) s (PCRE_DOTALL) You already know that the '_' and '#' characters were just pattern delimiters right? __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#5
|
|||
|
|||
|
Thanks for that. I know already for the regexp (.{0,}) being the same as (.*).
I was not aware of # or _ being pattern delimiters (I thought it was only /). The fact is I've got sometime a text with end of line caracters (\n) in it. Your regexp can handle them nicely with no problems, the other one cannot. But it's all good now. Thanks again for the help. |
Recent GIDBlog
Toyota - 2008 July Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Microsoft Visual C++6.0 - Illegal use of this type as an expression | RealmMaster | CPP / C++ Forum | 5 | 24-Aug-2006 13:51 |
| Regular Expressions question. | JdS | MySQL / PHP Forum | 3 | 04-Nov-2002 14:04 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The