GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Jul-2003, 17:58
EasyExpat EasyExpat is offline
New Member
 
Join Date: Jul 2003
Location: London
Posts: 3
EasyExpat is an unknown quantity at this point

preg_replace + regular expression


Hi,
I've got a flat file such as:
Quote:
<TITLE>Thursday 3rd March</TITLE><USER>Paul Smith</USER><ADDRESS>3, battersea square - SW12 3GE London - UK</ADDRESS>

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  
Old 08-Jul-2003, 23:46
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

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:

<?php
preg_match( '_<title>(.*)</title>_is', $file, $matches );
$title = $matches[1];
echo '$title is: '.$title;
?>


On the other hand, I wouldn't store my data like that in a text file.
  #3  
Old 09-Jul-2003, 13:42
EasyExpat EasyExpat is offline
New Member
 
Join Date: Jul 2003
Location: London
Posts: 3
EasyExpat is an unknown quantity at this point
Ok ok apologize!
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:

preg_match( '_<title>(.*)</title>_is', $file, $matches ); 



and
PHP Code:

preg_match("#<TITLE>(.{0,})</TITLE>#", $file, $matches); 


?
It gives exactly the same result (second one is an example I found from another forum)
  #4  
Old 09-Jul-2003, 21:40
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

PCRE pattern explanation


Actually Cyril, there's no difference...

this bit:
Code:
(.{0,}) is the same as (.*)
it just ends up looking more complicated for someone starting out with REGEXs. Why he gave you that I cannot explain... perhaps he was just trying to mess with your mind?

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:
<title>Thursday 3rd March</title><USER>Paul Smith</USER><ADDRESS>3, battersea square - SW12 3GE London - UK</ADDRESS>
that's done by the i modifier towards the end of the pattern.
i (PCRE_CASELESS)
s (PCRE_DOTALL)



You already know that the '_' and '#' characters were just pattern delimiters right?
  #5  
Old 10-Jul-2003, 05:36
EasyExpat EasyExpat is offline
New Member
 
Join Date: Jul 2003
Location: London
Posts: 3
EasyExpat is an unknown quantity at this point
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 GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft Visual C++6.0 - Illegal use of this type as an expression RealmMaster C++ Forum 5 24-Aug-2006 14:51
Regular Expressions question. JdS MySQL / PHP Forum 3 04-Nov-2002 15:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:13.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.