Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Small problem with regexp.
Old 09-17-2005, 03:31 AM Small problem with regexp.
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
Hi world!

Just need a tiny bit of help with this regexp:

PHP Code:
<?
$text 
"<open>¬!”£$%^&*()_+{}:@~?`-=[];’#<>,./</close>";
if (
preg_match("/<open>([^<]*)<\/close>/i",  $text,  $matches)) print $matches[1];
?>
The above regexp doesn't work because of the '<' that exists in the string. If i remove it, the regexp works:


PHP Code:
<?
$text 
"<open>¬!”£$%^&*()_+{}:@~?`-=[];’#>,./</close>";
if (
preg_match("/<open>([^<]*)<\/close>/i",  $text,  $matches)) print $matches[1];
?>
Any ideas why and any workarounds???

thanks
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
 
Register now for full access!
Old 09-17-2005, 07:41 AM
Novice Talker

Posts: 13
Trades: 0
What do you mean it doesn't work?(like does it throw an error, or does it just not match your pattern?) What type of patterns are you trying to match?
capiCrimm is offline
Reply With Quote
View Public Profile
 
Old 09-17-2005, 07:51 AM
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
Sorry for the lack of clarity.

It doesn't match the pattern!

I am trying to extract everything inbetween <open> and </close>.

It works fine up until it finds a '<' in the string.

Any ideas?
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 09-17-2005, 07:51 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
What about:
Code:
"/<open>([^<]*?)<\/close>/i"
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 09-17-2005, 07:59 AM
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
That's what i'm using... isn't it???
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 09-17-2005, 08:06 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
"/<open>([^<]*?)<\/close>/i"

There's an extra question mark that ought to put it in non-greedy mode...
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 09-17-2005, 09:34 AM
Skilled Talker

Posts: 64
Trades: 0
Can anyone explain what this code does?
Tomos is offline
Reply With Quote
View Public Profile
 
Old 09-17-2005, 09:37 AM
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
Thanks 0beron, it works.

But, for some reason, it is now spitting out:

�!��$%^&*()_+{}:@~?`-=[];�#<>,./

as the match!!! Any ideas why???
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 09-17-2005, 09:39 AM
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
Hi Tomos,

In a nut sheel, it checks the string to see if <open>some text here</close> exists and then extracts the "some text here".
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 09-17-2005, 05:12 PM
Skilled Talker

Posts: 64
Trades: 0
Oh right. So where would it be used?
Tomos is offline
Reply With Quote
View Public Profile
 
Old 09-17-2005, 05:32 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Quote:
Originally Posted by fambi
Thanks 0beron, it works.

But, for some reason, it is now spitting out:

�!��$%^&*()_+{}:@~?`-=[];�#<>,./

as the match!!! Any ideas why???
Not sure - might have something to do with the character set used to output the match? Try calling addslashes() or htmlentities() on the string before echoing it?

Tomos - this code uses a regular expression, which is basically a very compact and flexible way of finding specific things in text strings - here it is being used for picking out tags of some sort, but you can use them to check URLs and emails are valid, to match code numbers for an ordering system, to make sure usernames and form input don't have any dodgy characters or bits of script in them, the list goes on.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 09-17-2005, 05:38 PM
Skilled Talker

Posts: 64
Trades: 0
Oh right. Thanks 0beron!
Tomos is offline
Reply With Quote
View Public Profile
 
Old 09-18-2005, 02:37 AM
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
0beron - i thought the same and will try it out.

Tomos - this example can be used to extract the <title> of a webpage
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 09-18-2005, 04:10 AM
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
Quote:
Originally Posted by fambi
Thanks 0beron, it works.
Sorry 0beron! I have to take my words back... it doesn't seem to be working.

This is the full php script, nothing more and nothing less:
PHP Code:
<?
    $text 
"<open>¬!”£$%^&*()_+{}:@~?`-=[];’#<>,./</close>";
    if (
preg_match("/<open>([^<]*?)<\/close>/i",  $text,  $matches)) 
    {
        print 
"<pre>";
        
print_r($matches);
        print 
"</pre>";
    }
?>
If you have a few seconds, please try it out. I still have the same problem, i.e. if i remove the 'less than' sign it works, otherwise not.
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 09-18-2005, 11:15 PM
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
0beron! 0beron! Where art thou 0beron?
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Reply     « Reply to Small problem with regexp.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.16011 seconds with 12 queries