TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-21-2009, 01:08 PM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default Why does this preg_match fail?

Can someone plase explain why this statement fails?
PHP Code:
preg_match('/<h1>(*)<\/h1>/i'$line$matches); 
The failure results in the warning
Quote:
Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 5
If I enter this line instead (just to test the code), it works.
PHP Code:
preg_match('/<b class="main">(.*)<\/b>/i'$line$matches); 
benton is offline  
Reply With Quote
Old 12-21-2009, 04:37 PM   #2 (permalink)
The Visitor
 
Join Date: Dec 2009
Posts: 2
Thanks: 0
girvine is on a distinguished road
Default

Looks like your missing a dot.

In the first statement,
(*)
should be,
(.*)
girvine is offline  
Reply With Quote
Old 12-21-2009, 06:08 PM   #3 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Thanks. Unfortunately, that was just a typo made while editing the post. The original code already had it in there. I did find that using quotes instead of apostrophes made a difference though. So this now works:
PHP Code:
preg_match("/<td align=\"right\" valign=\"top\"><h1>(.*)<\/h1><\/td>/i"$line$matches); 
I had to add more code in the check to get it to work.

So the next step is to read that in from the database so it works without manual edits. This is what I am trying to do (the top line is simulating what would be read from the database):
PHP Code:
$search_str '<td align="right" valign="top"><h1>(.*)</h1></td>';
$search_str str_replace("/""\\/"$search_str);
$search_str str_replace('"''\\"'$search_str);
$condition '"/'.$search_str '/i"';

preg_match($condition$line$matches); 
That doesn't create any erros and when I look at the source, the $condition appears exactly as when I type it in directly but the match isn't found. Does anyone have any suggestions on what might make this fail?
benton is offline  
Reply With Quote
Old 12-21-2009, 07:16 PM   #4 (permalink)
The Visitor
 
Join Date: Dec 2009
Posts: 2
Thanks: 0
girvine is on a distinguished road
Default

If I take out the quotes around the condition and don't escape the quotes inside the condition,

PHP Code:
$search_str '<td align="right" valign="top"><h1>(.*)</h1></td>'
$search_str str_replace("/""\\/"$search_str); 
$condition '/'.$search_str '/i'

preg_match($condition$line$matches); 
it seems to work.
girvine is offline  
Reply With Quote
Old 12-21-2009, 11:57 PM   #5 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

That did it. Thanks for taking the time to help. It is much appreciated. :)
benton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Ternary = Fail delayedinsanity Absolute Beginners 3 11-01-2009 07:57 PM


All times are GMT. The time now is 02:17 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design