Results 1 to 10 of 16
Hybrid View
-
26th May 2011, 08:36 PM #1OPMember
A doubt about php's preg_match result.
So, I have this html file that basicaly says:
PHP Code:echo '<table>
<tr>
<td>Welcome to:</td>
<td><strong>Hell</strong></td>
</tr>
</table>';
Now, I have another php file that (should) gets (through CURL and preg_match) the "<strong>Hell</strong>" part:
Curl Result ends in the var: $Result
Pattern I want to match against the $Result: $pattern = '/ <td>Welcome to\:<\/td>
<td>(.*)<\/td>/m';
Preg Match result var: $pregResult
PHP Code:preg_match ($pattern, $Result, $pregResult)
PHP Code:array(0) { }
So, can anyone point me to what is going on wrong because I am out of ideas.
My setup is windows + XAMPP with phpversion 5.3.5.
PC
Here are the actual php files:
-------------- test.php ------------
PHP Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
echo '<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Welcome to:</td>
</tr>
<tr>
<td><strong>Hell</strong></td>
</tr>
</table>';
?>
</body>
</html>
PHP Code:<?php
error_reporting(-1);
$curlTarget = "http://127.0.0.1/multiline/test.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$curlTargget);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec ($ch);
curl_close($ch);
$patternCoiso = '/ <td>Welcome to:<\/td>
<td>(.*)<\/td>/mi';
preg_match($patternCoiso, $result, $match);
var_dump($match);
?>PCManiac Reviewed by PCManiac on . A doubt about php's preg_match result. So, I have this html file that basicaly says: echo '<table> <tr> <td>Welcome to:</td> <td><strong>Hell</strong></td> </tr> </table>'; (whether this code is or not valid, does not matter.. Is just of the sake of having an example) Rating: 5
-
26th May 2011, 08:43 PM #2MemberWebsite's:
bypassx.comI think that ":" not need to be scaped... try with:
Code:$pattern = '/ <td>Welcome to:<\/td> <td>(.*)<\/td>/m';
-
26th May 2011, 08:46 PM #3Respected Member
The Regex is fine, your preg_match order is messed up though. It should be:
PHP Code:preg_match($pattern, $Result, $pregResult);
-
26th May 2011, 08:51 PM #4OPMember
bleh.. sorry Loget, that's how I have coded.. I just messed it while retyping in to the post.
*fixes*
Meaning that is not the problem :\
Zeokat: you are right, it's not needed but being scaped won't (or shouldn't) change in anyway the result. i scaped it just to make sure it wasn't that that was messing it up. thanks for the reply though.
-
26th May 2011, 08:54 PM #5
any uninitialized variable in code?
Add this to the beginning of the code for debugging after the <?php tag
PHP Code:error_reporting(-1);
Trusted: Dom, l0calh0st, 0ccul7, robert420
Find all threads started by NewEraCracker
-
26th May 2011, 09:06 PM #6OPMember
No error output NEC. :\
I just copied/pasted the actual files to the main post.
-
26th May 2011, 11:54 PM #7Respected Member
Here is the code to do exactly what you want:
PHP Code:$Result = '<table>
<tr>
<td>Welcome to:</td>
<td><strong>Hell</strong></td>
</tr>
</table>';
$w = "[\s\S]*?";
$pattern = "/\<td>Welcome to:<\/td\>$w<td\>(.*)<\/td\>/";
preg_match($pattern, $Result, $pregResult);
var_dump($pregResult);
Code:array(2) { [0]=> string(57) "<td>Welcome to:</td> <td><strong>Hell</strong></td>" [1]=> string(21) "<strong>Hell</strong>" }
-
27th May 2011, 07:50 AM #8OPMember
Thanks for your replies guys.
Lockdown, It's weird but that does not work on my test environment... Am starting to think that the php version I have (or any of its libs) is buggy...
Mr Happy, I am not a programmer.. am just an entusiast would you point me to the right direction about parsing HTML with a XML parser?
-
27th May 2011, 12:19 AM #9Respected DeveloperWebsite's:
wrzc.orgYou can't parse [X]HTML with regex. Because HTML can't be parsed by regex. Regex is not a tool that can be used to correctly parse HTML. As I have answered in HTML-and-regex questions here so many times before, the use of regex will not allow you to consume HTML. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by HTML. HTML is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down HTML into its meaningful parts. so many times but it is not getting to me. Even enhanced irregular regular expressions as used by Perl are not up to the task of parsing HTML. You will never make me crack. HTML is a language of sufficient complexity that it cannot be parsed by regular expressions. Even Jon Skeet cannot parse HTML using regular expressions. Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing HTML with regex summons tainted souls into the realm of the living. HTML and regex go together like love, marriage, and ritual infanticide. The <center> cannot hold it is too late. The force of regex and HTML together in the same conceptual space will destroy your mind like so much watery putty. If you parse HTML with regex you are giving in to Them and their blasphemous ways which doom us all to inhuman toil for the One whose Name cannot be expressed in the Basic Multilingual Plane, he comes. HTML-plus-regexp will liquify the n?erves of the sentient whilst you observe, your psyche withering in the onslaught of horror. Rege???x-based HTML parsers are the cancer that is killing StackOverflow it is too late it is too late we cannot be saved the trangession of a chi?ld ensures regex will consume all living tissue (except for HTML which it cannot, as previously prophesied) dear lord help us how can anyone survive this scourge using regex to parse HTML has doomed humanity to an eternity of dread torture and security holes using regex as a tool to process HTML establishes a breach between this world and the dread realm of c??o??rrupt entities (like SGML entities, but more corrupt) a mere glimpse of the world of reg?<b>ex parsers for HTML will ins[/b]?tantly transport a programmer's consciousness into a world of ceaseless screaming, he comes[s], the pestilent sl[/s]ithy regex-infection wil?l devour your HT?ML parser, application and existence for all time like Visual Basic only worse he comes he comes do not fi?ght he com?e?s, ?h?i?s un?ho?ly radian?ce? destro?ying all enli??^?ghtenment, HTML tags lea?ki?n?g fr?o?m ?yo??ur eye?s? ?l?ik?e liq?uid pain, the song of re?gular exp?re[s]ssion parsing [/s]will exti?nguish the voices of mor?tal man from the sp?here I can see it can you see _????i^??t???_??_? it is beautiful t?he final snuffing of the lie?s of Man ALL IS LOS????????T ALL I?S LOST the pon?y he comes he c??om[s]es he co[/s][s]me[/s]s the ich?or permeates all MY FACE MY FACE ?h god no NO NOO?O?O NT stop the an?*??????????g????????l??????????e??s<code> ?a???r?????e</code> n?ot re`???a?l~??????? ZA????LG? IS?^??????_ TO???????? TH?E??? ?P???O??N?Y? H?????"????E????`????? ??????_???C???????_??O??????M????????_?E?????????O͇̹̺ͅƝ̴ȳ̳ TH̘?͖́̉ ͠P̯͍̭O̚N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ
Have you tried using an XML parser instead?
__________________________________________________ __________________
Doesn't look great here but the original answer to this type of question is the most popular reply ever on the worlds busiest coding site. It's the best answer I've ever seen to a coding question.
http://stackoverflow.com/questions/1...732454#1732454Tutorial How to SEO your Warez Site a guide to help you increase your organic traffic
Huge list of Warez Sites and free Multiposter Templates
-
27th May 2011, 01:35 AM #10MemberWebsite's:
EastsideHosting.com MediaONAIR.com ImgWiz.com cPadmin.net EastsidePCWorks.com HostedTalk.comPHP Code:preg_match ("/(<h1)(.*?)(>)(.*?)(<\/h1>)/i", $data, $match);
$found = $match[4];
So it's possible.
Might have to play with $matches (no pun intended) to find the match, 0-w/e prolly be 0-1EastsideHosting - (Web Hosting, Proxy Hosting, Reseller Program)
MediaONAIR - (SHOUTcast Servers, ICEcast Servers, Reseller Program)
Server Administrator - (Dedicated Servers, VPS Management, One Time Services)
ImgWiz - (Free Image Hosting)
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Looking for Exam Result Script
By DevilLnC in forum Web Application/Script SupportReplies: 5Last Post: 7th Feb 2014, 02:04 PM -
Need help with google search result
By .nItIsH. in forum Webmaster DiscussionReplies: 4Last Post: 4th Mar 2012, 07:54 AM -
preg_match help need!
By saninokia in forum Web Development AreaReplies: 8Last Post: 27th Nov 2011, 08:39 PM -
Why Google Result Going Down!!
By indianmoviefans.info in forum Webmaster DiscussionReplies: 13Last Post: 23rd Sep 2011, 08:00 AM -
Help with preg_match - php !
By softleaks in forum Webmaster DiscussionReplies: 5Last Post: 21st May 2011, 04:22 PM
themaManager - edit and manage...
Version 4.04 released. Open older version (or...