Jump to content

preg_match_all strange behavior


hrisafis

Recommended Posts

Hello to everybody

 

I have a problem with the following pattern

 

When I use this  preg_match_all('/<\a href="show_per.phtml\?id=(.*)" class="more_new">(.*)<\/a>/', $result, $shop_url, PREG_SET_ORDER );

everything seems to be fine, but when I send the pattern '/<\a href="show_per.phtml\?id=(.*)" class="more_new">(.*)<\/a>/'  through an html form and use this  preg_match_all($_POST['pattern'], $result, $shop_url, PREG_SET_ORDER );  nothing seems to work fine.

 

Why is that?

Any help will be really appriciated.

 

Thanks a lot.

Link to comment
https://forums.phpfreaks.com/topic/82650-preg_match_all-strange-behavior/
Share on other sites

can you post the html it would help me with the regexp

 

The part of the code that interests me is something like this.

<tr><td width="60%" class="new_color1" colspan="3"><span class="products">VIA P4M900, CORE 2, VGA, DDR-1, DDR-2, S-ATA1, LAN 10/100, AUDIO-6</span></td> <td class="new_color1" align="right"><b> 41.90 €</b> </td></tr><tr></tr> <tr><td class="new_color1" align="left" width="60%">Για σύγκριση επιλέξτε <input type="Checkbox" name="check1" value="PER.523596" onclick="checkclick('check1')">   <a href="show_per.phtml?id=PER.523596" class="more_new">περισσότερα >></a>  </td>

  <td class="new_color1" colspan="4" align="right">

<a href="basket.phtml?itemid=PER.523596&itemname=&itemprice=41.90&itemquant=1" class="main">Προσθήκη στο καλάθι

<img src="clipartfds/basket2.gif" width="20" height="15" alt="Προσθήκη στο καλάθι" border="0"></a>

</td></tr>

</table>

</td></tr>

<tr><td> </td></tr>

<tr><td><a href="show_per.phtml?id=PER.526673"><img src="images/PER/VSMALL/PER.526673.jpg"  alt="" border="0"></a></td><td>

<table border="0" cellpadding="0" cellspacing="0" width="100%">

<tr><td width="60%" class="new_color" colspan="3"><span class="per"><b> PER.526673 :</b></span>  

<a href="show_per.phtml?id=PER.526673" class=blue_link><b>GIGABYTE 945GZM-S2</b></a>  <br><a href="search_per.phtml?category=%CC%C7%D4%D1%C9%CA%C7+%CA%C1%D1%D4%C1&category2=SOCKET+775" style="text-decoration: none"><font style="bold; font-style: normal; color: #909090;">(ΜΗΤΡΙΚΗ ΚΑΡΤΑ - SOCKET 775)</font></a></td>

<td class="new_color" align="right"><span class="small">Τιμή: 38.57 € <br>+ 19% ΦΠΑ: 7.33 € </span></td>

    </tr>

were is the closing tag for the href anyways try this

 

preg_match_all("/<a href=\"show_per.phtml\?id=([^\"]+)\"[^>]+>([^<]+)/",$strText,$arrMatches);

print_r($arrMatches);

 

 

$strText is the actual string from curl

 

 

Nop not working again, I have to remind you that the pattern was working fine, it was when I sended the pattern as a variable through a form that I was not getting results.

 

sorry I mean echo it you said you tried to send it through a form maybe extra slashes were added

 

Yes it could be any number of things, that your magic quotes settings add slashes automatically to post strings is probably a good guess though. A good general rule of thumb in any debugging is to validate/check your inputs/ouputs that you're getting at runtime. This means echoing out stuff.

The pattern looks fine:

http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=gdoa6b3t

 

Here's a function to auto escape slashes if neccesary:

<?php
function stripslash($str) {
if (get_magic_quotes_gpc()) {
	$str = stripslashes($str);
}
return $str;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.