Regular Expression Preg_replace problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wama
Forum Newbie
Posts: 3
Joined: Wed Dec 22, 2004 3:06 am

Regular Expression Preg_replace problem

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
wama
Forum Newbie
Posts: 3
Joined: Wed Dec 22, 2004 3:06 am

Post 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>
wama
Forum Newbie
Posts: 3
Joined: Wed Dec 22, 2004 3:06 am

Post 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 :)
Post Reply