Page 1 of 1

Regular Expression Preg_replace problem

Posted: Wed Dec 22, 2004 3:14 am
by wama
Good day All,

i am using this code to remove all links to the specified pages in the page showing to the user. first i start output buffering and then i do a preg_replace, when i try removing just the link it works fine. But i also need sometimes to remove the surronding Table row, to get rid of the borders & wasted space. This is for use in a show/hide module. the code is:

Code: Select all

// names of php files seperated by '|' with no '.php'
    $pages = "StockTransfers|ReverseGRN";

    $buffer= preg_replace("<&#1111;aA]s?.*s&#1111;hH]&#1111;rR]&#1111;eE]&#1111;fF]=&#1111;"']?.*(".$pages.").*&#1111;"']?s*.*&#1111;^(/a)]+/a>","dummy",$buffer);
    return $buffer;
the new trial -removing td and tr tags - is:

Code: Select all

$pages = "StockTransfers|ReverseGRN|SelectOrderItems";

    $buffer= preg_replace("(<&#1111;tT]&#1111;rR]\s&#1111;^>]*>&#1111;\s]*<&#1111;tT]&#1111;dD]\s&#1111;^>]*>)?<&#1111;aA]\s?.*\s&#1111;hH]&#1111;rR]&#1111;eE]&#1111;fF]=&#1111;"'']?.*(".$pages.").*&#1111;"'']?\s*.*&#1111;^(\/a)]+/a>(</&#1111;tT]&#1111;dD]>&#1111;\s]*</&#1111;tT]&#1111;rR]>)?","dummy",$buffer);
the result is an empty page!

Please advice, thank you very much in advance :D

Posted: Wed Dec 22, 2004 3:57 am
by onion2k
I reckon this one..

Code: Select all

/<tr>&#1111;\s]*<td>&#1111;\s]*<a href=&#1111;"'].*&#1111;"']>.*</a>&#1111;\s]*</td>&#1111;\s]*</tr>/i
..should do the same job. You'd need some brackets in there if you're trying to keep stuff.

This may help: http://weitz.de/regex-coach/
Its an interactive reg-ex builder. You put your text in the lower window, and write your regex in the top one, and it highlights what will be matched on the fly.

Posted: Wed Dec 22, 2004 5:00 am
by wama
thnx alot fot the link ..i am installing it now, i did get one error, i forgot to add the delimitier

now the problem i have is with the optional part .. the TR and TD tags... my guess would be that this has something to do with some limitation about the \n and the \r ..not sure?

Target Html:

Code: Select all

<tr>
							<td class="menu_group_item">
								<a href="http://localhost/web-erp/web-erp/SelectSalesOrder.php?"><li>Outstanding Sales Orders Maintenance</li></a>							</td>
							</tr>
							<tr>
Any advice ?

using the code stated here:

Code: Select all

$buffer= preg_replace("/(<&#1111;tT]&#1111;rR].*>(.|\r|\n|\r\n)*<&#1111;tT]&#1111;dD].*>)?<&#1111;aA]\s?.*\s&#1111;hH]&#1111;rR]&#1111;eE]&#1111;fF]=&#1111;"'']?.*(".$pages.").*&#1111;"'']?\s*.*&#1111;^(\/a)]+\/a>(<\/&#1111;tT]&#1111;dD]>(.|\r|\n|\r\n)*<\/&#1111;tT]&#1111;rR]>)?/","",$buffer);
the output is:

Code: Select all

<tr>
							<td class="menu_group_item">
															</td>
							</tr>
							<tr>

Posted: Wed Dec 22, 2004 6:40 am
by wama
Thanks .problem resolved

the problem was in that i did use the "." as a represntive for char / num & spaces, while it doesn't support Spaces! so i just added "\s*" and voila :)